DefaultEventHandler   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 165
Duplicated Lines 15.76 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 26
loc 165
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setIo() 0 4 1
A addPreDispatchEventHandler() 13 13 3
A addPostDispatchEventHandler() 13 13 3
A clearEventHandlers() 0 7 1
A onPreDispatchEvent() 0 19 2
A onPostDispatchEvent() 0 9 2
A objectToPropertyArray() 0 21 4
A eventToString() 0 11 2
A getSubscribedEvents() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Handler;
13
14
use Cubiche\Core\EventBus\Event\EventInterface;
15
use Cubiche\Core\EventBus\Event\EventSubscriberInterface;
16
use Cubiche\Core\EventDispatcher\PostDispatchEvent;
17
use Cubiche\Core\EventDispatcher\PreDispatchEvent;
18
use Webmozart\Console\Api\IO\IO;
19
use Webmozart\Console\UI\Component\Table;
20
use Webmozart\Console\UI\Style\TableStyle;
21
22
/**
23
 * DefaultEventHandler class.
24
 *
25
 * @author Ivannis Suárez Jerez <[email protected]>
26
 */
27
class DefaultEventHandler implements EventSubscriberInterface, EventHandlerInterface
28
{
29
    /**
30
     * @var callback
31
     */
32
    protected $preDispatchHandler;
33
34
    /**
35
     * @var callback
36
     */
37
    protected $postDispatchHandler;
38
39
    /**
40
     * @var IO
41
     */
42
    protected $io;
43
44
    /**
45
     * @param IO $io
46
     */
47
    public function setIo(IO $io)
48
    {
49
        $this->io = $io;
50
    }
51
52
    /**
53
     * @param callable $handler
54
     *
55
     * @return $this
56
     */
57 View Code Duplication
    public function addPreDispatchEventHandler($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...
58
    {
59
        if (!is_callable($handler)) {
60
            throw new \InvalidArgumentException(sprintf(
61
                'Expected a callable. Got: %s',
62
                is_object($handler) ? get_class($handler) : gettype($handler)
63
            ));
64
        }
65
66
        $this->preDispatchHandler = $handler;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param callable $handler
73
     *
74
     * @return $this
75
     */
76 View Code Duplication
    public function addPostDispatchEventHandler($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...
77
    {
78
        if (!is_callable($handler)) {
79
            throw new \InvalidArgumentException(sprintf(
80
                'Expected a callable. Got: %s',
81
                is_object($handler) ? get_class($handler) : gettype($handler)
82
            ));
83
        }
84
85
        $this->postDispatchHandler = $handler;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @return $this
92
     */
93
    public function clearEventHandlers()
94
    {
95
        $this->preDispatchHandler = null;
96
        $this->postDispatchHandler = null;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param PreDispatchEvent $preDispatchEvent
103
     */
104
    public function onPreDispatchEvent(PreDispatchEvent $preDispatchEvent)
105
    {
106
        if ($this->preDispatchHandler) {
107
            call_user_func($this->preDispatchHandler, $preDispatchEvent->event(), $this->io);
108
        } else {
109
            $this->io->writeLine('<c1>------------------------------------------------------------------------</c1>');
110
            $this->io->writeLine('Dispatching <c1>'.$this->eventToString($preDispatchEvent->event()).'</c1> with:');
0 ignored issues
show
Compatibility introduced by
$preDispatchEvent->event() of type object<Cubiche\Core\Even...patcher\EventInterface> is not a sub-type of object<Cubiche\Core\Even...s\Event\EventInterface>. It seems like you assume a child interface of the interface Cubiche\Core\EventDispatcher\EventInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
111
            $this->io->writeLine('');
112
113
            $properties = $this->objectToPropertyArray($preDispatchEvent->event());
0 ignored issues
show
Compatibility introduced by
$preDispatchEvent->event() of type object<Cubiche\Core\Even...patcher\EventInterface> is not a sub-type of object<Cubiche\Core\Even...s\Event\EventInterface>. It seems like you assume a child interface of the interface Cubiche\Core\EventDispatcher\EventInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
114
115
            $table = new Table(TableStyle::asciiBorder());
116
            $table->setHeaderRow(array_keys($properties));
117
            $table->addRow(array_values($properties));
118
119
            $table->render($this->io);
120
            $this->io->writeLine('');
121
        }
122
    }
123
124
    /**
125
     * @param PostDispatchEvent $postDispatchEvent
126
     */
127
    public function onPostDispatchEvent(PostDispatchEvent $postDispatchEvent)
128
    {
129
        if ($this->postDispatchHandler) {
130
            call_user_func($this->postDispatchHandler, $postDispatchEvent->event(), $this->io);
131
        } else {
132
            $this->io->writeLine('<c1>'.$this->eventToString($postDispatchEvent->event()).'</c1> success!!');
0 ignored issues
show
Compatibility introduced by
$postDispatchEvent->event() of type object<Cubiche\Core\Even...patcher\EventInterface> is not a sub-type of object<Cubiche\Core\Even...s\Event\EventInterface>. It seems like you assume a child interface of the interface Cubiche\Core\EventDispatcher\EventInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
133
            $this->io->writeLine('');
134
        }
135
    }
136
137
    /**
138
     * @param EventInterface $event
139
     *
140
     * @return array
141
     */
142
    protected function objectToPropertyArray(EventInterface $event)
143
    {
144
        $properties = array();
145
        $reflection = new \ReflectionClass(get_class($event));
146
147
        /** @var \ReflectionProperty $property */
148
        foreach ($reflection->getProperties() as $property) {
149
            $property->setAccessible(true);
150
151
            if (!empty($property->getValue($event))) {
152
                $value = $property->getValue($event);
153
                if ($value instanceof \DateTime) {
154
                    $value = $value->format(\DateTime::ISO8601);
155
                }
156
157
                $properties[$property->getName()] = $value;
158
            }
159
        }
160
161
        return $properties;
162
    }
163
164
    /**
165
     * @param EventInterface $event
166
     *
167
     * @return array
168
     */
169
    protected function eventToString(EventInterface $event)
170
    {
171
        $pos = strrpos($event->eventName(), '\\');
172
        if ($pos !== false) {
173
            $className = substr($event->eventName(), strrpos($event->eventName(), '\\') + 1);
174
        } else {
175
            $className = $event->eventName();
176
        }
177
178
        return strtolower(trim(preg_replace('([A-Z])', ' $0', $className)));
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public static function getSubscribedEvents()
185
    {
186
        return array(
187
            PreDispatchEvent::class => 'onPreDispatchEvent',
188
            PostDispatchEvent::class => 'onPostDispatchEvent',
189
        );
190
    }
191
}
192