1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/orm package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Orm\Repository\QueryObject; |
11
|
|
|
|
12
|
|
|
use Slick\Database\RecordList; |
13
|
|
|
use Slick\Database\Sql\Select; |
14
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorInterface; |
15
|
|
|
use Slick\Orm\Entity\EntityCollection; |
16
|
|
|
use Slick\Orm\Mapper\EventTriggers; |
17
|
|
|
use Slick\Orm\Orm; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Select Event Trigger methods |
21
|
|
|
* |
22
|
|
|
* @package Slick\Orm\Repository\QueryObject |
23
|
|
|
* @author Filipe Silva <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait SelectEventTriggers |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Use event triggers |
30
|
|
|
*/ |
31
|
|
|
use EventTriggers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Emit before select event |
35
|
|
|
* |
36
|
|
|
* @param Select $query |
37
|
|
|
* @param EntityDescriptorInterface $entityDescriptor |
38
|
|
|
* |
39
|
|
|
* @return \League\Event\EventInterface|string |
40
|
|
|
*/ |
41
|
2 |
View Code Duplication |
public function triggerBeforeSelect( |
|
|
|
|
42
|
|
|
Select $query, EntityDescriptorInterface $entityDescriptor |
43
|
|
|
) { |
44
|
2 |
|
$event = new \Slick\Orm\Event\Select( |
45
|
2 |
|
null, |
46
|
|
|
[ |
47
|
2 |
|
'query' => $query, |
48
|
1 |
|
'entityDescriptor' => $entityDescriptor |
49
|
1 |
|
] |
50
|
1 |
|
); |
51
|
2 |
|
$event->setAction(\Slick\Orm\Event\Select::ACTION_BEFORE_SELECT); |
52
|
2 |
|
return Orm::getEmitter($this->getEntityClassName()) |
53
|
2 |
|
->emit($event); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Emits the after select event |
58
|
|
|
* |
59
|
|
|
* @param RecordList $data |
60
|
|
|
* @param EntityCollection $entity |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @return \League\Event\EventInterface|string |
63
|
|
|
*/ |
64
|
2 |
View Code Duplication |
public function triggerAfterSelect($data, EntityCollection $entities) |
|
|
|
|
65
|
|
|
{ |
66
|
2 |
|
$event = new \Slick\Orm\Event\Select( |
67
|
2 |
|
null, |
68
|
|
|
[ |
69
|
2 |
|
'data' => $data, |
70
|
1 |
|
'entityCollection' => $entities |
71
|
1 |
|
] |
72
|
1 |
|
); |
73
|
2 |
|
$event->setAction(\Slick\Orm\Event\Select::ACTION_AFTER_SELECT); |
74
|
2 |
|
return Orm::getEmitter($this->getEntityClassName()) |
75
|
2 |
|
->emit($event); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.