Skip to content

Commit ba2d251

Browse files
committed
fix optional+custom entities
1 parent 8fbd06b commit ba2d251

35 files changed

Lines changed: 371 additions & 140 deletions

‎src/Domain/DomainIdFactoryInterface.php‎

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\Domain\Infra\Bundle;
6+
7+
use MsgPhp\Domain\{CommandBusInterface, EventBusInterface};
8+
use MsgPhp\Domain\Infra\SimpleBus\{DomainCommandBus, DomainEventBus};
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
11+
12+
/**
13+
* @author Roland Franssen <franssen.roland@gmail.com>
14+
*/
15+
final class ServiceConfigHelper
16+
{
17+
public static function configureSimpleBus(ContainerConfigurator $container): void
18+
{
19+
static $configured = false;
20+
21+
if ($configured) {
22+
return;
23+
}
24+
25+
$container->services()
26+
->defaults()
27+
->private()
28+
->set(DomainCommandBus::class)
29+
->args([ref('command_bus')])
30+
->alias(CommandBusInterface::class, DomainCommandBus::class)
31+
->set(DomainEventBus::class)
32+
->args([ref('event_bus')])
33+
->alias(EventBusInterface::class, DomainEventBus::class)
34+
;
35+
36+
$configured = true;
37+
}
38+
39+
private function __construct()
40+
{ }
41+
}

‎src/Domain/Infra/Doctrine/DomainEntityRepositoryTrait.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
trait DomainEntityRepositoryTrait
1919
{
2020
private $em;
21+
private $class;
2122

22-
public function __construct(EntityManagerInterface $em)
23+
public function __construct(EntityManagerInterface $em, string $class)
2324
{
2425
$this->em = $em;
26+
$this->class = $class;
2527
}
2628

2729
private function doFind($id, ...$idN)

‎src/Domain/Infra/InMemory/DomainEntityRepositoryTrait.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
trait DomainEntityRepositoryTrait
1515
{
1616
private $memory;
17+
private $class;
1718

18-
public function __construct(array $memory)
19+
public function __construct(array $memory, string $class)
1920
{
2021
$this->memory = $memory;
22+
$this->class = $class;
2123
}
2224

2325
private function doFind($id, ...$idN)
@@ -113,7 +115,6 @@ private function getEntityField($entity, $field)
113115

114116
private function createResultSet(int $offset = null, int $limit = null, callable $filter = null): DomainEntityCollection
115117
{
116-
// @todo $filter as first arg
117118
$memory = null == $filter ? $this->memory : array_values(array_filter($this->memory, $filter));
118119

119120
if (null !== $offset || null !== $limit) {

src/Domain/Infra/MessageBus/DomainCommandBus.php renamed to src/Domain/Infra/SimpleBus/DomainCommandBus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace MsgPhp\Domain\Infra\MessageBus;
5+
namespace MsgPhp\Domain\Infra\SimpleBus;
66

77
use MsgPhp\Domain\CommandBusInterface;
88
use SimpleBus\Message\Bus\MessageBus;

src/Domain/Infra/MessageBus/DomainEventBus.php renamed to src/Domain/Infra/SimpleBus/DomainEventBus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace MsgPhp\Domain\Infra\MessageBus;
5+
namespace MsgPhp\Domain\Infra\SimpleBus;
66

77
use MsgPhp\Domain\EventBusInterface;
88
use SimpleBus\Message\Bus\MessageBus;

‎src/Domain/Tests/Infra/Doctrine/DomainEntityRepositoryTraitTest.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private function createRepository(array $entities = [])
137137
{
138138
$em = $this->createEntityManager($entities);
139139

140-
return new class($em) {
140+
return new class($em, TestEntity::class) {
141141
use DomainEntityRepositoryTrait {
142142
doFind as public;
143143
doFindByFields as public;
@@ -149,7 +149,6 @@ private function createRepository(array $entities = [])
149149
createQueryBuilder as public;
150150
}
151151

152-
private $class = TestEntity::class;
153152
private $alias = 'test_entity';
154153
private $idFields = ['id'];
155154
};

‎src/Domain/Tests/Infra/InMemory/DomainEntityRepositoryTraitTest.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,12 @@ public function testCompositePrimaryFields()
148148
$entityA2 = $this->createEntity('A', ['token' => '2']),
149149
$entityB1 = $this->createEntity('B', ['token' => '1']),
150150
];
151-
$repository = new class($entities) {
151+
$repository = new class($entities, \stdClass::class) {
152152
use DomainEntityRepositoryTrait {
153153
doFind as public;
154154
doExists as public;
155155
}
156156

157-
private $class = \stdClass::class;
158157
private $idFields = ['id', 'token'];
159158
};
160159

@@ -173,7 +172,7 @@ public function testCompositePrimaryFields()
173172

174173
private function createRepository(array $entities = [])
175174
{
176-
return new class($entities) {
175+
return new class($entities, \stdClass::class) {
177176
use DomainEntityRepositoryTrait {
178177
doFind as public;
179178
doFindByFields as public;
@@ -184,7 +183,6 @@ private function createRepository(array $entities = [])
184183
createResultSet as public;
185184
}
186185

187-
private $class = \stdClass::class;
188186
private $idFields = ['id'];
189187
};
190188
}

‎src/User/Command/CreateUserCommand.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
final class CreateUserCommand
1313
{
14-
/** @var UserIdInterface */
14+
/** @var UserIdInterface|null */
1515
public $userId;
1616

1717
/** @var string */
@@ -26,7 +26,7 @@ final class CreateUserCommand
2626
/** @var bool */
2727
public $plainPassword;
2828

29-
public function __construct(UserIdInterface $userId, string $email, string $password, bool $enable = false, bool $plainPassword = true)
29+
public function __construct(?UserIdInterface $userId, string $email, string $password, bool $enable = false, bool $plainPassword = true)
3030
{
3131
$this->userId = $userId;
3232
$this->email = $email;

‎src/User/Command/Handler/AddUserRoleHandler.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use MsgPhp\Domain\EventBusInterface;
88
use MsgPhp\User\Command\AddUserRoleCommand;
9-
use MsgPhp\User\Entity\UserRole;
109
use MsgPhp\User\Event\UserRoleAddedEvent;
1110
use MsgPhp\User\Repository\UserRepositoryInterface;
1211
use MsgPhp\User\Repository\UserRoleRepositoryInterface;
12+
use MsgPhp\User\UserFactory;
1313

1414
/**
1515
* @author Roland Franssen <franssen.roland@gmail.com>
@@ -18,18 +18,20 @@ final class AddUserRoleHandler
1818
{
1919
private $userRepository;
2020
private $userRoleRepository;
21+
private $factory;
2122
private $eventBus;
2223

23-
public function __construct(UserRepositoryInterface $userRepository, UserRoleRepositoryInterface $userRoleRepository, EventBusInterface $eventBus)
24+
public function __construct(UserRepositoryInterface $userRepository, UserRoleRepositoryInterface $userRoleRepository, UserFactory $factory, EventBusInterface $eventBus)
2425
{
2526
$this->userRepository = $userRepository;
2627
$this->userRoleRepository = $userRoleRepository;
28+
$this->factory = $factory;
2729
$this->eventBus = $eventBus;
2830
}
2931

3032
public function handle(AddUserRoleCommand $command): void
3133
{
32-
$userRole = new UserRole($this->userRepository->find($command->userId), $command->role);
34+
$userRole = $this->factory->createUserRole($this->userRepository->find($command->userId), $command->role);
3335

3436
$this->userRoleRepository->save($userRole);
3537
$this->eventBus->handle(new UserRoleAddedEvent($userRole));

0 commit comments

Comments
 (0)