Completed
Push — master ( 5017d8...561e2a )
by Christian
07:36 queued 03:33
created

AclCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 106
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A inject() 0 4 1
A execute() 0 15 2
A renderNode() 0 14 3
A formatNode() 0 4 1
A recursiveSort() 0 10 3
A compareNodes() 0 11 3
1
<?php
2
3
namespace N98\Magento\Command\Config\Data;
4
5
use Magento\Framework\Acl\AclResource\Config\Reader\Filesystem as AclConfigReader;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use PBergman\Console\Helper\TreeHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class AclCommand extends AbstractMagentoCommand
12
{
13
    /**
14
     * @var AclConfigReader
15
     */
16
    private $configReader;
17
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('config:data:acl')
22
            ->setDescription('Prints acl.xml data as table')
23
        ;
24
    }
25
26
    /**
27
     * @param AclConfigReader $configReader
28
     */
29
    public function inject(AclConfigReader $configReader)
30
    {
31
        $this->configReader = $configReader;
32
    }
33
34
    /**
35
     * @param InputInterface  $input
36
     * @param OutputInterface $output
37
     *
38
     * @return int|void
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $data = $this->configReader->read();
43
44
        $tree = new TreeHelper();
45
        $tree->setTitle('ACL Tree');
46
47
        $this->recursiveSort($data['config']['acl']['resources']);
48
49
        foreach ($data['config']['acl']['resources'] as $row) {
50
            $this->renderNode($tree, $row);
51
        }
52
53
        $tree->printTree($output);
54
    }
55
56
    /**
57
     * @param TreeHelper $tree
58
     * @param array      $row
59
     */
60
    protected function renderNode($tree, $row)
61
    {
62
        $tree = $tree->newNode($this->formatNode($row));
63
64
        foreach ($row['children'] as $child) {
65
            if (count($child['children']) > 0) {
66
                $this->renderNode($tree, $child);
67
            } else {
68
                $tree->addValue($this->formatNode($child));
69
            }
70
        }
71
72
        $tree->end();
73
    }
74
75
    /**
76
     * @param $row
77
     *
78
     * @return string
79
     */
80
    private function formatNode($row)
81
    {
82
        return sprintf('%d: <info>%s</info> [<comment>%s</comment>]', $row['sortOrder'], $row['title'], $row['id']);
83
    }
84
85
    /**
86
     * @param $array
87
     */
88
    public function recursiveSort(array &$array)
89
    {
90
        uasort($array, [$this, 'compareNodes']);
91
92
        foreach ($array as &$row) {
93
            if (count($row['children']) > 0) {
94
                $this->recursiveSort($row['children']);
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param array $a
101
     * @param array $b
102
     *
103
     * @return int
104
     */
105
    private function compareNodes(array $a, array $b)
106
    {
107
        if ($a['sortOrder'] > $b['sortOrder']) {
108
            return 1;
109
        }
110
        if ($a['sortOrder'] < $b['sortOrder']) {
111
            return -1;
112
        }
113
114
        return 0;
115
    }
116
}
117