Skip to content

Commit 2a932d3

Browse files
committed
Add options
1 parent c851b6c commit 2a932d3

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@
7878
"webklex/php-imap": "^6.2",
7979
"ext-imap": "*",
8080
"tatevikgr/rss-feed": "dev-main",
81-
"ext-pdo": "*"
81+
"ext-pdo": "*",
82+
"ext-gd": "*"
8283
},
8384
"require-dev": {
8485
"phpunit/phpunit": "^9.5",

config/services/repositories.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ services:
6060
arguments:
6161
- PhpList\Core\Domain\Subscription\Model\SubscriberAttributeValue
6262
PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeDefinitionRepository:
63-
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
6463
arguments:
65-
- PhpList\Core\Domain\Subscription\Model\SubscriberAttributeDefinition
64+
$entityManager: '@doctrine.orm.entity_manager'
65+
$dynamicListAttrRepository: '@PhpList\Core\Domain\Subscription\Repository\DynamicListAttrRepository'
6666
PhpList\Core\Domain\Subscription\Repository\SubscriptionRepository:
6767
parent: PhpList\Core\Domain\Common\Repository\AbstractRepository
6868
arguments:

src/Domain/Subscription/Model/SubscriberAttributeDefinition.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpList\Core\Domain\Common\Model\AttributeTypeEnum;
99
use PhpList\Core\Domain\Common\Model\Interfaces\DomainModel;
1010
use PhpList\Core\Domain\Common\Model\Interfaces\Identity;
11+
use PhpList\Core\Domain\Subscription\Model\Dto\DynamicListAttrDto;
1112
use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeDefinitionRepository;
1213

1314
#[ORM\Entity(repositoryClass: SubscriberAttributeDefinitionRepository::class)]
@@ -39,6 +40,14 @@ class SubscriberAttributeDefinition implements DomainModel, Identity
3940
#[ORM\Column(name: 'tablename', type: 'string', length: 255, nullable: true)]
4041
private ?string $tableName = null;
4142

43+
/**
44+
* Doctrine does NOT map this property.
45+
* It exists only for runtime usage (e.g. dynamic attribute options).
46+
* Doctrine will ignore it completely.
47+
* @var DynamicListAttrDto[]
48+
*/
49+
private array $options = [];
50+
4251
public function getId(): ?int
4352
{
4453
return $this->id;
@@ -120,4 +129,17 @@ public function setTableName(?string $tableName): self
120129
$this->tableName = $tableName;
121130
return $this;
122131
}
132+
133+
/** @param DynamicListAttrDto[] $options */
134+
public function setOptions(array $options): self
135+
{
136+
$this->options = $options;
137+
return $this;
138+
}
139+
140+
/** @return DynamicListAttrDto[] */
141+
public function getOptions(): array
142+
{
143+
return $this->options;
144+
}
123145
}

src/Domain/Subscription/Repository/SubscriberAttributeDefinitionRepository.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpList\Core\Domain\Subscription\Repository;
66

7+
use Doctrine\ORM\EntityManagerInterface;
78
use PhpList\Core\Domain\Common\Repository\AbstractRepository;
89
use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait;
910
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
@@ -13,6 +14,50 @@ class SubscriberAttributeDefinitionRepository extends AbstractRepository impleme
1314
{
1415
use CursorPaginationTrait;
1516

17+
public function __construct(
18+
EntityManagerInterface $entityManager,
19+
private readonly DynamicListAttrRepository $dynamicListAttrRepository,
20+
) {
21+
parent::__construct(
22+
$entityManager,
23+
$entityManager->getClassMetadata(SubscriberAttributeDefinition::class)
24+
);
25+
}
26+
27+
/**
28+
* @param SubscriberAttributeDefinition[] $defs
29+
* @return SubscriberAttributeDefinition[]
30+
*/
31+
private function hydrateOptionsForAll(array $defs): array
32+
{
33+
foreach ($defs as $def) {
34+
$this->hydrateOptions($def);
35+
}
36+
return $defs;
37+
}
38+
39+
private function hydrateOptions(SubscriberAttributeDefinition $def): void
40+
{
41+
$table = $def->getTableName();
42+
if ($table) {
43+
$options = $this->dynamicListAttrRepository->getAll($table);
44+
$def->setOptions($options);
45+
}
46+
}
47+
48+
public function getAfterId(int $lastId, int $limit): array
49+
{
50+
$result = $this->createQueryBuilder('e')
51+
->andWhere('e.id > :lastId')
52+
->setParameter('lastId', $lastId)
53+
->orderBy('e.id', 'ASC')
54+
->setMaxResults($limit)
55+
->getQuery()
56+
->getResult();
57+
58+
return $this->hydrateOptionsForAll($result);
59+
}
60+
1661
public function findOneByName(string $name): ?SubscriberAttributeDefinition
1762
{
1863
return $this->findOneBy(['name' => $name]);

src/Domain/Subscription/Service/Manager/AttributeDefinitionManager.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ public function create(AttributeDefinitionDto $attributeDefinitionDto): Subscrib
5757

5858
$this->definitionRepository->persist($attributeDefinition);
5959

60+
$insertedOptions = [];
6061
if ($tableName) {
6162
$this->dynamicTablesManager->createOptionsTableIfNotExists($tableName);
62-
$this->dynamicListAttrManager->insertOptions($tableName, $attributeDefinitionDto->options);
63+
$insertedOptions = $this->dynamicListAttrManager->insertOptions(
64+
$tableName,
65+
$attributeDefinitionDto->options
66+
);
6367
}
6468

65-
return $attributeDefinition;
69+
return $attributeDefinition->setOptions($insertedOptions);
6670
}
6771

6872
public function update(
@@ -85,15 +89,16 @@ public function update(
8589
->setRequired($attributeDefinitionDto->required)
8690
->setDefaultValue($attributeDefinitionDto->defaultValue);
8791

92+
$syncedOptions = [];
8893
if ($attributeDefinitionDto->type && $attributeDefinitionDto->type->isMultiValued()) {
8994
$tableName = $attributeDefinition->getTableName() ?? $this->dynamicTablesManager
9095
->resolveTableName(name: $attributeDefinitionDto->name, type: $attributeDefinitionDto->type);
9196
$this->dynamicTablesManager->createOptionsTableIfNotExists($tableName);
9297

93-
$this->dynamicListAttrManager->syncOptions($tableName, $attributeDefinitionDto->options);
98+
$syncedOptions = $this->dynamicListAttrManager->syncOptions($tableName, $attributeDefinitionDto->options);
9499
}
95100

96-
return $attributeDefinition;
101+
return $attributeDefinition->setOptions($syncedOptions);
97102
}
98103

99104
public function delete(SubscriberAttributeDefinition $attributeDefinition): void

0 commit comments

Comments
 (0)