Completed
Push — master ( ea0efc...8ffed2 )
by Cheren
04:19
created

EventManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 0
cbo 5
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadListeners() 0 23 5
A trigger() 0 9 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Event;
17
18
use Core\Plugin;
19
use Cake\Core\App;
20
use Cake\Event\Event;
21
use Cake\Event\EventManager as CakeEventManager;
22
23
/**
24
 * Class Manager
25
 *
26
 * @package Core\Event
27
 */
28
class EventManager extends CakeEventManager
29
{
30
31
    /**
32
     * Load Event Handlers during bootstrap.
33
     *
34
     * Plugins can add their own custom EventHandler in Config/events.php
35
     * with the following format:
36
     *
37
     * 'events' => [
38
     *      'Core.CoreEventHandler' => [
39
     *          'options' = [
40
     *              'callable' => '',
41
     *              'priority' => '',
42
     *          ]
43
     *      ]
44
     * ]
45
     *
46
     * @return void
47
     */
48
    public static function loadListeners()
49
    {
50
        $manager = self::instance();
51
        $plugins = Plugin::loaded();
52
53
        foreach ($plugins as $plugin) {
0 ignored issues
show
Bug introduced by
The expression $plugins of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
54
            $events = Plugin::getData($plugin, 'events')->getArrayCopy();
55
            foreach ($events as $name => $config) {
56
                if (is_numeric($name)) {
57
                    $name   = $config;
58
                    $config = [];
59
                }
60
61
                $class  = App::className($name, 'Event');
62
                $config = (array) $config;
63
64
                if ($class !== false) {
65
                    $listener = new $class($config);
66
                    $manager->on($listener, $config);
67
                }
68
            }
69
        }
70
    }
71
72
    /**
73
     * Emits an event.
74
     *
75
     * @param string $name
76
     * @param object|null $subject
77
     * @param array|null $data
78
     * @return Event
79
     */
80
    public static function trigger($name, $subject = null, $data = null)
81
    {
82
        $event = new Event($name, $subject, $data);
83
        if (is_object($subject)) {
84
            return $subject->eventManager()->dispatch($event);
85
        }
86
87
        return EventManager::instance()->dispatch($event);
88
    }
89
}
90