Completed
Push — master ( f735ec...441321 )
by Martijn van
02:11
created

ListRouteCommand::routes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
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 = array('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 = array();
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
30
            $handlers = $this->getUrlHandlersForController($controller);
31
32
            $actions  = $handlers ? $handlers : $this->getActionsForController($controller);
33
34
            $list[$route] = array($route, $controller, implode("\n", $actions));
35
        }
36
37
        ksort($list);
38
39
        return $list;
40
    }
41
42
    /**
43
     * @param string $controller
44
     * @return array
45
     */
46
    protected function getActionsForController($controller)
47
    {
48
        $actions = $this->getValuesOrKeysFromConfig($controller, 'allowed_actions');
49
50
        foreach ($actions as $key => $action) {
51
            if($action == 'index') unset($actions[$key]);
52
        }
53
54
        return $actions;
55
    }
56
57
    /**
58
     * @param string $controller
59
     * @return array
60
     */
61
    protected function getUrlHandlersForController($controller)
62
    {
63
        $handlers = $this->getValuesOrKeysFromConfig($controller, 'url_handlers');
64
65
        foreach ($handlers as $key => $handler) {
66
            if($handler == '') unset($handlers[$key]);
67
        }
68
69
        return $handlers;
70
    }
71
72
    /**
73
     * @param string $controller
74
     * @param string $config
75
     * @return array
76
     */
77
    protected function getValuesOrKeysFromConfig($controller, $config = 'allowed_actions')
78
    {
79
        $values = (array)Config::inst()->get($controller, $config, Config::UNINHERITED);
80
81
        if(!isset($values[0])) { // assoc with permissions set as values
82
            $values = array_keys($values);
83
        }
84
85
        return $values;
86
    }
87
}
88