EventManagerAwareTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 4 2
A getEventsManager() 0 15 5
A setEventsManager() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Traits;
6
7
use Phalcon\Di;
8
use Phalcon\Events\ManagerInterface as EventsManager;
0 ignored issues
show
Bug introduced by
The type Phalcon\Events\ManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Phalcon\Traits\EventManagerAwareTrait.
12
 *
13
 * Trait for event processing
14
 *
15
 * @package Phalcon\Traits
16
 */
17
18
trait EventManagerAwareTrait
19
{
20
    /**
21
     * @var EventsManager
22
     */
23
    protected $eventsManager = null;
24
25
    /**
26
     * set event manager.
27
     *
28
     * @param EventsManager $eventsManager
29
     */
30
    public function setEventsManager(EventsManager $manager)
31
    {
32
        $this->eventsManager = $manager;
33
    }
34
35
    /**
36
     * return event manager.
37
     *
38
     * @return EventsManager | null
39
     */
40
    public function getEventsManager()
41
    {
42
        $di = Di::getDefault();
43
44
        if (!empty($this->eventsManager)) {
45
            $manager = $this->eventsManager;
46
        } elseif ($di->has('eventsManager')) {
47
            $manager = $di->get('eventsManager');
48
        }
49
50
        if (isset($manager) && $manager instanceof EventsManager) {
51
            return $manager;
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * Checking if event manager is defined - fire event.
59
     *
60
     * @param string $event
61
     * @param object $source
62
     * @param mixed $data
63
     * @param boolean $cancelable
64
     *
65
     */
66
    public function fire($event, $source, $data = null, $cancelable = true)
67
    {
68
        if ($manager = $this->getEventsManager()) {
69
            $manager->fire($event, $source, $data, $cancelable);
70
        }
71
    }
72
}
73