ListRouteCommand::getActionsForController()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
class ListRouteCommand extends SilverstripeCommand
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    /**
6
     * @var string
7
     */
8
    protected $signature = 'list:route';
9
10
    /**
11
     * @var string
12
     */
13
    protected $description = 'List all routes to Controllers';
14
15
    public function fire()
16
    {
17
        $headers = ['Route', 'Controller', 'Allowed Actions'];
18
19
        $this->table($headers, $this->routes());
20
    }
21
22
    public function routes()
23
    {
24
        $routes = Config::inst()->get('Director', 'rules');
25
26
        $list = [];
27
28
        foreach ($routes as $route => $controller) {
0 ignored issues
show
Bug introduced by
The expression $routes of type array|integer|double|string|boolean 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...
29
            $handlers = $this->getUrlHandlersForController($controller);
30
31
            $actions = $handlers ? $handlers : $this->getActionsForController($controller);
32
33
            $list[$route] = [$route, $controller, implode("\n", $actions)];
34
        }
35
36
        ksort($list);
37
38
        return $list;
39
    }
40
41
    /**
42
     * @param string $controller
43
     *
44
     * @return array
45
     */
46
    protected function getActionsForController($controller)
47
    {
48
        $actions = (array) $this->getValuesOrKeysFromConfig($controller, 'allowed_actions');
49
50
        foreach ($actions as $key => $action) {
51
            if ($action == 'index') {
52
                unset($actions[$key]);
53
            }
54
        }
55
56
        return $actions;
57
    }
58
59
    /**
60
     * @param string $controller
61
     *
62
     * @return array
63
     */
64
    protected function getUrlHandlersForController($controller)
65
    {
66
        $handlers = (array) $this->getValuesOrKeysFromConfig($controller, 'url_handlers');
67
68
        foreach ($handlers as $key => $handler) {
69
            if ($handler == '') {
70
                unset($handlers[$key]);
71
            }
72
        }
73
74
        return $handlers;
75
    }
76
77
    /**
78
     * @param string $controller
79
     * @param string $config
80
     *
81
     * @return array
82
     */
83
    protected function getValuesOrKeysFromConfig($controller, $config = 'allowed_actions')
84
    {
85
        $values = (array) Config::inst()->get($controller, $config, Config::UNINHERITED);
86
87
        if (!isset($values[0])) { // assoc with permissions set as values
88
            $values = array_keys($values);
89
        }
90
91
        return $values;
92
    }
93
}
94