1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4webDomain\Service; |
4
|
|
|
|
5
|
|
|
use T4webDomain\Event; |
6
|
|
|
use T4webDomain\Exception\EntityNotFoundException; |
7
|
|
|
use T4webDomainInterface\Infrastructure\CriteriaInterface; |
8
|
|
|
use T4webDomainInterface\EntityInterface; |
9
|
|
|
use T4webDomainInterface\ServiceInterface; |
10
|
|
|
use T4webDomainInterface\Infrastructure\RepositoryInterface; |
11
|
|
|
use T4webDomainInterface\EventManagerInterface; |
12
|
|
|
|
13
|
|
|
class Deleter implements ServiceInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @var RepositoryInterface |
18
|
|
|
*/ |
19
|
|
|
protected $repository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EventManagerInterface |
23
|
|
|
*/ |
24
|
|
|
protected $eventManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param RepositoryInterface $repository |
28
|
|
|
*/ |
29
|
|
|
public function __construct(RepositoryInterface $repository, EventManagerInterface $eventManager = null) |
30
|
|
|
{ |
31
|
|
|
$this->repository = $repository; |
32
|
|
|
$this->eventManager = $eventManager; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return EntityInterface |
37
|
|
|
*/ |
38
|
|
|
public function handle($filter, $changes) |
39
|
|
|
{ |
40
|
|
|
/** @var CriteriaInterface $criteria */ |
41
|
|
|
$criteria = $this->repository->createCriteria($filter); |
42
|
|
|
$entity = $this->repository->find($criteria); |
43
|
|
|
|
44
|
|
|
if (!$entity) { |
45
|
|
|
throw new EntityNotFoundException("Entity does not found."); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($this->eventManager) { |
49
|
|
|
$event = $this->eventManager->createEvent('delete.pre', $entity); |
50
|
|
|
$this->eventManager->trigger($event); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->repository->remove($entity); |
54
|
|
|
|
55
|
|
|
if ($this->eventManager) { |
56
|
|
|
$event = $this->eventManager->createEvent('delete.post', $entity); |
57
|
|
|
$this->eventManager->trigger($event); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $entity; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $filter |
65
|
|
|
* @return EntityInterface[]|null |
66
|
|
|
*/ |
67
|
|
|
public function deleteAll(array $filter = []) |
68
|
|
|
{ |
69
|
|
|
$criteria = $this->repository->createCriteria($filter); |
70
|
|
|
$entities = $this->repository->findMany($criteria); |
71
|
|
|
|
72
|
|
|
if (empty($entities)) { |
73
|
|
|
throw new EntityNotFoundException("Entities does not found."); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
foreach ($entities as $entity) { |
77
|
|
|
$this->repository->remove($entity); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $entities; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|