1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Console\Api\Config; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\EventBus\Event\EventBus; |
15
|
|
|
use Cubiche\Core\EventBus\Event\EventSubscriberInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* CommandConfig trait. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
trait CommandConfigTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EventBus |
26
|
|
|
*/ |
27
|
|
|
protected $eventBus; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var callback |
31
|
|
|
*/ |
32
|
|
|
protected $preDispatchHandler = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callback |
36
|
|
|
*/ |
37
|
|
|
protected $postDispatchHandler = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $className; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param EventBus $eventBus |
46
|
|
|
*/ |
47
|
|
|
public function setEventBus(EventBus $eventBus) |
48
|
|
|
{ |
49
|
|
|
$this->eventBus = $eventBus; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $className |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setClass($className) |
58
|
|
|
{ |
59
|
|
|
$this->className = $className; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function className() |
68
|
|
|
{ |
69
|
|
|
return $this->className; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param callable $handler |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
* |
77
|
|
|
* @throws \InvalidArgumentException |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function onPreDispatchEvent($handler) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (!is_callable($handler)) { |
82
|
|
|
throw new \InvalidArgumentException(sprintf( |
83
|
|
|
'Expected a callable. Got: %s', |
84
|
|
|
is_object($handler) ? get_class($handler) : gettype($handler) |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->preDispatchHandler = $handler; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param callable $handler |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
* |
98
|
|
|
* @throws \InvalidArgumentException |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function onPostDispatchEvent($handler) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
if (!is_callable($handler)) { |
103
|
|
|
throw new \InvalidArgumentException(sprintf( |
104
|
|
|
'Expected a callable. Got: %s', |
105
|
|
|
is_object($handler) ? get_class($handler) : gettype($handler) |
106
|
|
|
)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->postDispatchHandler = $handler; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return callable |
116
|
|
|
*/ |
117
|
|
|
public function preDispatchEventHandler() |
118
|
|
|
{ |
119
|
|
|
return $this->preDispatchHandler; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return callable |
124
|
|
|
*/ |
125
|
|
|
public function postDispatchEventHandler() |
126
|
|
|
{ |
127
|
|
|
return $this->postDispatchHandler; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param EventSubscriberInterface $subscriber |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function addEventSubscriber(EventSubscriberInterface $subscriber) |
136
|
|
|
{ |
137
|
|
|
if ($this->eventBus === null) { |
138
|
|
|
throw new \RuntimeException('The event bus should not be null'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->eventBus->addSubscriber($subscriber); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.