Skip to content

Commit 1eeb128

Browse files
authored
finetune EAV domain (#145)
1 parent 04e98d9 commit 1eeb128

36 files changed

+339
-117
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ cache:
5656
- $HOME/.composer/cache/files/
5757
- var/cache/
5858

59+
before_cache:
60+
- rm -rf $HOME/.composer/cache/files/msgphp
61+
5962
branches:
6063
only:
6164
- master

UPGRADE-0.4.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# UPGRADE FROM 0.3 to 0.4
22

3+
## Eav
4+
5+
- Refactored `Entity\Fields\AttributeValueField` into `Entity\Features\EntityAttributeValue`
6+
- Added `Infra\Doctrine\Repository\EntityAttributeValueRepositoryTrait`
7+
- Added default domain messages (`Command\` and `Event\`)
8+
9+
## User
10+
11+
- Renamed `UserAttributeValue::getAttributeValueId()` to `getId()`
12+
- Added `UserAttributeValue::changeValue()`
13+
- Deleted `UserAttributeValue::getAttributeValue()`
14+
- Renamed `ChangeUserAttributeValueCommand::$attributeValueId` to `$id`
15+
- Renamed `DeleteUserAttributeValueCommand::$attributeValueId` to `$id`
16+
317
## UserBundle
418

519
- Added support for Symfony Messenger and is favored over SimpleBus by default

docs/reference/entities.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ Class | Abstract
2727

2828
### Entity Fields
2929

30-
- `MsgPhp\Eav\Entity\Fields\AttributeValueField`
3130
- `MsgPhp\Eav\Entity\Fields\AttributesField`
3231

3332
### Entity Features
3433

35-
- No features available
34+
- `MsgPhp\Eav\Entity\Features\EntityAttributeValue`
3635

3736
## `msgphp/user`
3837

src/Domain/Infra/Doctrine/DomainEntityRepositoryTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private function addFieldCriteria(QueryBuilder $qb, array $fields, bool $or = fa
168168

169169
$expr = $qb->expr();
170170
$where = $or ? $expr->orX() : $expr->andX();
171-
$alias = $alias ?? $qb->getAllAliases()[0] ?? $this->alias;
171+
$alias = $alias ?? $this->alias;
172172
$idFields = array_flip($this->identityHelper->getIdentifierFieldNames($this->class));
173173
$associations = $this->em->getClassMetadata($this->class)->getAssociationMappings();
174174

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Eav\Command;
6+
7+
/**
8+
* @author Roland Franssen <[email protected]>
9+
*/
10+
class CreateAttributeCommand
11+
{
12+
public $context;
13+
14+
final public function __construct(array $context)
15+
{
16+
$this->context = $context;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Eav\Command;
6+
7+
/**
8+
* @author Roland Franssen <[email protected]>
9+
*/
10+
class DeleteAttributeCommand
11+
{
12+
public $attributeId;
13+
14+
final public function __construct($attributeId)
15+
{
16+
$this->attributeId = $attributeId;
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Eav\Command\Handler;
6+
7+
use MsgPhp\Domain\Factory\EntityAwareFactoryInterface;
8+
use MsgPhp\Domain\Message\{DomainMessageBusInterface, MessageDispatchingTrait};
9+
use MsgPhp\Eav\Command\CreateAttributeCommand;
10+
use MsgPhp\Eav\Entity\Attribute;
11+
use MsgPhp\Eav\Event\AttributeCreatedEvent;
12+
use MsgPhp\Eav\Repository\AttributeRepositoryInterface;
13+
14+
/**
15+
* @author Roland Franssen <[email protected]>
16+
*/
17+
final class CreateAttributeHandler
18+
{
19+
use MessageDispatchingTrait;
20+
21+
private $repository;
22+
23+
public function __construct(EntityAwareFactoryInterface $factory, DomainMessageBusInterface $bus, AttributeRepositoryInterface $repository)
24+
{
25+
$this->factory = $factory;
26+
$this->bus = $bus;
27+
$this->repository = $repository;
28+
}
29+
30+
public function __invoke(CreateAttributeCommand $command): void
31+
{
32+
$attribute = $this->factory->create(Attribute::class, $command->context + ['id' => $this->factory->nextIdentifier(Attribute::class)]);
33+
34+
$this->repository->save($attribute);
35+
$this->dispatch(AttributeCreatedEvent::class, [$attribute]);
36+
}
37+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Eav\Command\Handler;
6+
7+
use MsgPhp\Domain\Exception\EntityNotFoundException;
8+
use MsgPhp\Domain\Factory\EntityAwareFactoryInterface;
9+
use MsgPhp\Domain\Message\{DomainMessageBusInterface, MessageDispatchingTrait};
10+
use MsgPhp\Eav\Command\DeleteAttributeCommand;
11+
use MsgPhp\Eav\Entity\Attribute;
12+
use MsgPhp\Eav\Event\AttributeDeletedEvent;
13+
use MsgPhp\Eav\Repository\AttributeRepositoryInterface;
14+
15+
/**
16+
* @author Roland Franssen <[email protected]>
17+
*/
18+
final class DeleteAttributeHandler
19+
{
20+
use MessageDispatchingTrait;
21+
22+
private $repository;
23+
24+
public function __construct(EntityAwareFactoryInterface $factory, DomainMessageBusInterface $bus, AttributeRepositoryInterface $repository)
25+
{
26+
$this->factory = $factory;
27+
$this->bus = $bus;
28+
$this->repository = $repository;
29+
}
30+
31+
public function __invoke(DeleteAttributeCommand $command): void
32+
{
33+
try {
34+
$attribute = $this->repository->find($this->factory->identify(Attribute::class, $command->attributeId));
35+
} catch (EntityNotFoundException $e) {
36+
return;
37+
}
38+
39+
$this->repository->delete($attribute);
40+
$this->dispatch(AttributeDeletedEvent::class, [$attribute]);
41+
}
42+
}

src/Eav/Entity/Fields/AttributeValueField.php renamed to src/Eav/Entity/Features/EntityAttributeValue.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@
22

33
declare(strict_types=1);
44

5-
namespace MsgPhp\Eav\Entity\Fields;
5+
namespace MsgPhp\Eav\Entity\Features;
66

77
use MsgPhp\Eav\{AttributeIdInterface, AttributeValueIdInterface};
88
use MsgPhp\Eav\Entity\{Attribute, AttributeValue};
99

1010
/**
1111
* @author Roland Franssen <[email protected]>
1212
*/
13-
trait AttributeValueField
13+
trait EntityAttributeValue
1414
{
1515
/** @var AttributeValue */
1616
private $attributeValue;
1717

18-
public function getAttributeValue(): AttributeValue
19-
{
20-
return $this->attributeValue;
21-
}
22-
23-
public function getAttributeValueId(): AttributeValueIdInterface
18+
public function getId(): AttributeValueIdInterface
2419
{
2520
return $this->attributeValue->getId();
2621
}
@@ -39,4 +34,9 @@ public function getValue()
3934
{
4035
return $this->attributeValue->getValue();
4136
}
37+
38+
public function changeValue($value): void
39+
{
40+
$this->attributeValue->changeValue($value);
41+
}
4242
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Eav\Event;
6+
7+
use MsgPhp\Eav\Entity\Attribute;
8+
9+
/**
10+
* @author Roland Franssen <[email protected]>
11+
*/
12+
class AttributeCreatedEvent
13+
{
14+
public $attribute;
15+
16+
final public function __construct(Attribute $attribute)
17+
{
18+
$this->attribute = $attribute;
19+
}
20+
}

0 commit comments

Comments
 (0)