1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Sirius\Orm\Behaviour; |
5
|
|
|
|
6
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
use Sirius\Orm\Contract\EntityInterface; |
8
|
|
|
use Sirius\Orm\Event\DeletedEntity; |
9
|
|
|
use Sirius\Orm\Event\DeletingEntity; |
10
|
|
|
use Sirius\Orm\Event\NewEntity; |
11
|
|
|
use Sirius\Orm\Event\NewMapperQuery; |
12
|
|
|
use Sirius\Orm\Event\SavedEntity; |
13
|
|
|
use Sirius\Orm\Event\SavingEntity; |
14
|
|
|
use Sirius\Orm\Mapper; |
15
|
|
|
use Sirius\Orm\Query; |
16
|
|
|
|
17
|
|
|
class Events implements BehaviourInterface |
18
|
|
|
{ |
19
|
|
|
protected $mapperName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EventDispatcherInterface |
23
|
|
|
*/ |
24
|
|
|
protected $events; |
25
|
|
|
|
26
|
|
|
public function __construct(EventDispatcherInterface $events, string $mapperName) |
27
|
|
|
{ |
28
|
|
|
$this->events = $events; |
29
|
|
|
$this->mapperName = $mapperName; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getName() |
33
|
|
|
{ |
34
|
|
|
return 'events'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function onNewQuery(Mapper $mapper, Query $query) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$event = new NewMapperQuery($this->mapperName, $query); |
40
|
|
|
$this->events->dispatch($event); |
41
|
|
|
|
42
|
|
|
return $event->getQuery(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function onNewEntity(Mapper $mapper, EntityInterface $entity) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$event = new NewEntity($this->mapperName, $entity); |
48
|
|
|
$this->events->dispatch($event); |
49
|
|
|
|
50
|
|
|
return $event->getEntity(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function onSaving(Mapper $mapper, EntityInterface $entity) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$event = new SavingEntity($this->mapperName, $entity); |
56
|
|
|
$this->events->dispatch($event); |
57
|
|
|
|
58
|
|
|
return $event->getEntity(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function onSaved(Mapper $mapper, EntityInterface $entity) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$event = new SavedEntity($this->mapperName, $entity); |
64
|
|
|
$this->events->dispatch($event); |
65
|
|
|
|
66
|
|
|
return $event->getEntity(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function onDeleting(Mapper $mapper, EntityInterface $entity) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$event = new DeletingEntity($this->mapperName, $entity); |
72
|
|
|
$this->events->dispatch($event); |
73
|
|
|
|
74
|
|
|
return $event->getEntity(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function onDeleted(Mapper $mapper, EntityInterface $entity) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$event = new DeletedEntity($this->mapperName, $entity); |
80
|
|
|
$this->events->dispatch($event); |
81
|
|
|
|
82
|
|
|
return $event->getEntity(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.