These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace C4tech\RayEmitter\Domain; |
||
2 | |||
3 | use C4tech\RayEmitter\Contracts\Domain\Aggregate as AggregateInterface; |
||
4 | use C4tech\RayEmitter\Contracts\Domain\Command as CommandInterface; |
||
5 | use C4tech\RayEmitter\Contracts\Domain\Event as EventInterface; |
||
6 | use C4tech\RayEmitter\Contracts\Event\Collection as EventCollectionInterface; |
||
7 | use C4tech\RayEmitter\Exceptions\CommandHandlerMissing; |
||
8 | use C4tech\RayEmitter\Exceptions\EventHandlerMissing; |
||
9 | use Illuminate\Support\Facades\Config; |
||
10 | |||
11 | abstract class Aggregate implements AggregateInterface |
||
12 | { |
||
13 | /** |
||
14 | * Aggregate root entity. |
||
15 | * @var AggregateRootInterface |
||
16 | */ |
||
17 | protected $root; |
||
18 | |||
19 | /** |
||
20 | * Event sequence counter. |
||
21 | * @var integer |
||
22 | */ |
||
23 | protected $sequence = -1; |
||
24 | |||
25 | /** |
||
26 | * @inheritDoc |
||
27 | */ |
||
28 | 2 | View Code Duplication | public function apply(EventInterface $event) |
0 ignored issues
–
show
|
|||
29 | { |
||
30 | 2 | $method = $this->createMethodName('apply', $event); |
|
31 | |||
32 | 2 | if (!method_exists($this, $method)) { |
|
33 | 1 | throw new EventHandlerMissing( |
|
34 | 1 | sprintf( |
|
35 | 1 | 'Command %s does not have a handler for its expected aggregate %s', |
|
36 | 1 | get_class($event), |
|
37 | 1 | get_class($this) |
|
38 | 1 | ), |
|
39 | 501 |
||
40 | 1 | ); |
|
41 | } |
||
42 | |||
43 | 1 | $this->$method($event); |
|
44 | 1 | } |
|
45 | |||
46 | /** |
||
47 | * Create Method Name |
||
48 | * |
||
49 | * Generate a method name using a configurable prefix and an object's class basename. |
||
50 | * @param string $prefix_key The key from the configuration to lookup the appropriate prefix. |
||
51 | * @param object $object The object which shall be reduced to its class basename. |
||
52 | * @param string $prefix_default The default prefix. |
||
53 | * @return string The method name to use. |
||
54 | */ |
||
55 | 2 | protected function createMethodName($prefix_key, $object, $prefix_default = null) |
|
56 | { |
||
57 | 2 | if (!$prefix_default) { |
|
58 | 1 | $prefix_default = $prefix_key; |
|
59 | 1 | $prefix_key .= '_prefix'; |
|
60 | 1 | } |
|
61 | |||
62 | 2 | $prefix = Config::get('ray_emitter.' . $prefix_key, $prefix_default); |
|
63 | 2 | $base = class_basename($object); |
|
64 | |||
65 | 2 | return $prefix . $base; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | 1 | public function getEntity() |
|
72 | { |
||
73 | 1 | return $this->root->makeEntity(); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @inheritDoc |
||
78 | */ |
||
79 | 1 | public function getId() |
|
80 | { |
||
81 | 1 | return $this->root->getId(); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @inheritDoc |
||
86 | */ |
||
87 | 3 | public function getSequence() |
|
88 | { |
||
89 | 3 | return $this->sequence; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @inheritDoc |
||
94 | */ |
||
95 | 2 | View Code Duplication | public function handle(CommandInterface $command) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
96 | { |
||
97 | 2 | $method = $this->createMethodName('handle', $command); |
|
98 | |||
99 | 2 | if (!method_exists($this, $method)) { |
|
100 | 1 | throw new CommandHandlerMissing( |
|
101 | 1 | sprintf( |
|
102 | 1 | 'Command %s does not have a handler for its expected aggregate %s', |
|
103 | 1 | get_class($command), |
|
104 | 1 | get_class($this) |
|
105 | 1 | ), |
|
106 | 501 |
||
107 | 1 | ); |
|
108 | } |
||
109 | |||
110 | 1 | return $this->$method($command); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | public function hydrate(EventCollectionInterface $events) |
||
117 | { |
||
118 | 1 | $events->each(function (EventInterface $event) { |
|
119 | 1 | $this->apply($event); |
|
120 | 1 | $this->sequence++; |
|
121 | 1 | }); |
|
122 | 1 | } |
|
123 | |||
124 | /** |
||
125 | * Magic Getter |
||
126 | * |
||
127 | * Expose getter methods on the Aggregate and Aggregate Root as properties. |
||
128 | * @param string $property Requested "property" |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 2 | public function __get($property) |
|
132 | { |
||
133 | 2 | $method = 'get' . studly_case($property); |
|
134 | |||
135 | 2 | if (method_exists($this, $method)) { |
|
136 | 1 | return $this->$method(); |
|
137 | } |
||
138 | |||
139 | 1 | return $this->root->$property; |
|
140 | } |
||
141 | } |
||
142 |
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.