Test Failed
Push — master ( ffc597...4abc3b )
by Julien
12:58 queued 09:01
created

EventsAwareTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 62.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 44
c 1
b 0
f 0
dl 0
loc 153
ccs 27
cts 43
cp 0.6279
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setEventSlug() 0 3 1
A setEventsManager() 0 3 1
B fireSet() 0 51 10
A getEventsManager() 0 12 5
A fire() 0 6 2
A getEventsSlug() 0 8 1
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Events;
12
13
use Phalcon\Di;
14
use Phalcon\Events\Manager;
15
use Phalcon\Events\ManagerInterface;
16
use Zemit\Exception;
17
use Zemit\Utils\Slug;
18
19
/**
20
 * Trait EventsAwareTrait
21
 *
22
 * @author Julien Turbide <[email protected]>
23
 * @copyright Zemit Team <[email protected]>
24
 *
25
 * @since 1.0
26
 * @version 1.0
27
 *
28
 * @package Zemit\Events
29
 */
30
trait EventsAwareTrait
31
{
32
    /**
33
     * @var Manager
34
     */
35
    protected $eventsManager = null;
36
    
37
    /**
38
     * Set Event Manager Service Provider
39
     *
40
     * @param Manager $eventsManager
41
     */
42 4
    public function setEventsManager(ManagerInterface $manager)
43
    {
44 4
        $this->eventsManager = $manager;
0 ignored issues
show
Documentation Bug introduced by
$manager is of type Phalcon\Events\ManagerInterface, but the property $eventsManager was declared to be of type Phalcon\Events\Manager. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
45
    }
46
    
47
    /**
48
     * return event manager
49
     *
50
     * @return Manager|null
51
     */
52 4
    public function getEventsManager() : ?ManagerInterface
53
    {
54 4
        if (!empty($this->eventsManager)) {
55 4
            $manager = $this->eventsManager;
56
        } elseif (Di::getDefault()->has('eventsManager')) {
57
            $manager = Di::getDefault()->get('eventsManager');
58
        }
59 4
        if (isset($manager) && $manager instanceof Manager) {
60 4
            return $manager;
61
        }
62
        
63
        return null;
64
    }
65
    
66
    /**
67
     * Event slug to use as a component
68
     * my-component:beforeSomeTask
69
     * my-component:afterSomeTask
70
     *
71
     * @var null|string
72
     */
73
    public static $_eventsSlug = null;
74
    
75
    /**
76
     * Return the event component slug
77
     *
78
     * @return null|string Component slug
79
     * @throws \Zemit\Exception
80
     */
81 4
    public static function getEventsSlug()
82
    {
83
        return
84 4
            self::$_eventsSlug ??
85
            self::$_eventsSlug =
86 1
                Slug::generate(
87 1
                    basename(
88 4
                        str_replace('\\', '/', __CLASS__)
89
                    )
90
                );
91
    }
92
    
93
    /**
94
     * Set the event component slug
95
     *
96
     * @param $eventSlug
97
     */
98
    public static function setEventSlug($eventSlug)
99
    {
100
        self::$_eventsSlug = $eventSlug;
101
    }
102
    
103
    /**
104
     * Checking if event manager is defined - fire event
105
     *
106
     * @param $task
107
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
108
     * @param bool $cancelable
109
     *
110
     * @throws Exception
111
     */
112 4
    public function fire($task, $data = null, $cancelable = false)
113
    {
114 4
        if ($manager = $this->getEventsManager()) {
115 4
            $manager->fire($this->getEventsSlug() . ':' . $task, $this, $data, $cancelable);
116
        } else {
117
            throw new Exception('Events Manager Service Provider \'eventsManager\' does not exists in DI of \'' . get_class($this) . '\'');
118
        }
119
    }
120
    
121
    /**
122
     * Add possibility to parse an holder and run a callback after
123
     *
124
     * @param $holder
125
     * @param null $class
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $class is correct as it would always require null to be passed?
Loading history...
126
     * @param array $params
127
     * @param null $callback
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $callback is correct as it would always require null to be passed?
Loading history...
128
     *
129
     * @return mixed|null
130
     * @throws Exception
131
     */
132 4
    public function fireSet(&$holder, $class = null, array $params = [], $callback = null)
133
    {
134
        // prepare event name
135 4
        $event = basename(str_replace('\\', '//', $class));
136
        
137
        // fire before event with the holder
138 4
        $this->fire('before' . $event, func_get_args());
139
        
140
        // holder not set, apply class to it
141 4
        if (!isset($holder)) {
142
            // can be a class path
143 4
            if (class_exists($class)) {
144 4
                $holder = new $class(...$params);
145
            }
146
            // can be a callable
147
            elseif (is_callable($class)) {
148
                $holder = $class(...$params);
149
            }
150
            // can be the object
151
            elseif (is_object($class)) {
152
                $holder = $class;
153
            }
154
            // class not found
155
            elseif (is_string($class)) {
156
                throw new Exception('Class "' . $class . '" not found');
157
            }
158
            // other error
159
            else {
160 4
                throw new Exception('Unknown type "' . $class . '" for "$class"');
161
            }
162 4
        } elseif (is_string($holder)) {
163
            // can be a class path
164
            if (class_exists($holder)) {
165
                $holder = new $holder(...$params);
166
            }
167
            // class not founmd
168
            else {
169
                throw new Exception('Class "' . $class . '" not found');
170
            }
171
        }
172
        
173
        // run the callback if isset
174 4
        if (isset($callback) && is_callable($callback)) {
175 4
            $callback($this);
176
        }
177
        
178
        // fire after event
179 4
        $this->fire('after' . $event, func_get_args());
180
        
181
        // return the holder
182 4
        return $holder;
183
    }
184
}
185