Completed
Push — master ( 5cde88...2c9d27 )
by Cheren
02:25
created

CoreEventHandler::_onSetupAdmin()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
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 JBZoo\Utils\FS;
20
use Cake\Event\Event;
21
use Core\Controller\AppController;
22
use Cake\Event\EventListenerInterface;
23
24
/**
25
 * Class CoreEventHandler
26
 *
27
 * @package Core\Event
28
 */
29
class CoreEventHandler implements EventListenerInterface
30
{
31
32
    /**
33
     * Returns a list of events this object is implementing.
34
     *
35
     * @return array
36
     */
37
    public function implementedEvents()
38
    {
39
        return [
40
            'Controller.setup' => 'onControllerSetup'
41
        ];
42
    }
43
44
    /**
45
     * @param Event $event
46
     */
47
    public function onControllerSetup(Event $event)
48
    {
49
        /** @var AppController $controller */
50
        $controller = $event->subject();
51
        if ($controller->request->param('prefix') == 'admin') {
52
            $this->_onSetupAdmin($controller);
53
        }
54
    }
55
56
    /**
57
     * Setup admin data.
58
     *
59
     * @param AppController $controller
60
     * @SuppressWarnings("unused")
61
     */
62
    protected function _onSetupAdmin(AppController $controller)
63
    {
64
        $plugins = Plugin::loaded();
65
        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...
66
            $path    = Plugin::path($plugin);
67
            $navConf = $path . 'config/admin_menu.php';
68
69
            if (FS::isFile($navConf)) {
70
                /** @noinspection PhpIncludeInspection */
71
                require_once $navConf;
72
            }
73
        }
74
    }
75
}
76