1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4webDomain\Service; |
4
|
|
|
|
5
|
|
|
use T4webDomainInterface\ServiceInterface; |
6
|
|
|
use T4webDomainInterface\Infrastructure\RepositoryInterface; |
7
|
|
|
use T4webDomainInterface\EntityFactoryInterface; |
8
|
|
|
use T4webDomainInterface\EventManagerInterface; |
9
|
|
|
|
10
|
|
|
class Creator implements ServiceInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var RepositoryInterface |
14
|
|
|
*/ |
15
|
|
|
protected $repository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var EntityFactoryInterface |
19
|
|
|
*/ |
20
|
|
|
protected $entityFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EventManagerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $eventManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RepositoryInterface $repository |
29
|
|
|
* @param EntityFactoryInterface $entityFactory |
30
|
|
|
* @param EventManagerInterface|null $eventManager |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
RepositoryInterface $repository, |
34
|
|
|
EntityFactoryInterface $entityFactory, |
35
|
|
|
EventManagerInterface $eventManager = null |
36
|
|
|
) { |
37
|
|
|
$this->repository = $repository; |
38
|
|
|
$this->entityFactory = $entityFactory; |
39
|
|
|
$this->eventManager = $eventManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function handle($criteria, $data) |
43
|
|
|
{ |
44
|
|
|
$entity = $this->entityFactory->create($data); |
45
|
|
|
|
46
|
|
|
if ($this->eventManager) { |
47
|
|
|
$event = $this->eventManager->createEvent('create.pre', $entity, $data); |
48
|
|
|
$this->eventManager->trigger($event); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->repository->add($entity); |
52
|
|
|
|
53
|
|
|
if ($this->eventManager) { |
54
|
|
|
$event = $this->eventManager->createEvent('create.post', $entity, $data); |
55
|
|
|
$this->eventManager->trigger($event); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $entity; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|