CommandConfigTrait::setEventBus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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)
0 ignored issues
show
Duplication introduced by
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...
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)
0 ignored issues
show
Duplication introduced by
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...
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