Command::getView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * @package File manager
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\file_manager\models;
11
12
use Exception;
13
use gplcart\core\Handler;
14
use gplcart\core\Hook;
15
use SplFileInfo;
16
17
/**
18
 * Manages basic behaviors and data related to File manager module
19
 */
20
class Command
21
{
22
23
    /**
24
     * Hook class instance
25
     * @var \gplcart\core\Hook $hook
26
     */
27
    protected $hook;
28
29
    /**
30
     * @param Hook $hook
31
     */
32
    public function __construct(Hook $hook)
33
    {
34
        $this->hook = $hook;
35
    }
36
37
    /**
38
     * Returns an array of supported commands
39
     * @return array
40
     */
41
    public function getHandlers()
42
    {
43
        $commands = &gplcart_static('module.file_manager.handlers');
44
45
        if (isset($commands)) {
46
            return $commands;
47
        }
48
49
        $commands = (array) gplcart_config_get(__DIR__ . '/../config/commands.php');
50
51
        foreach ($commands as $id => &$command) {
52
            $command['command_id'] = $id;
53
        }
54
55
        $this->hook->attach('module.file_manager.handlers', $commands, $this);
56
        return $commands;
57
    }
58
59
    /**
60
     * Returns a single command
61
     * @param string $command
62
     * @return array
63
     */
64
    public function get($command)
65
    {
66
        $commands = $this->getHandlers();
67
        return empty($commands[$command]) ? array() : $commands[$command];
68
    }
69
70
    /**
71
     * Returns an array of allowed commands for the given file
72
     * @param SplFileInfo|array $file
73
     * @return array
74
     */
75
    public function getAllowed($file)
76
    {
77
        $commands = array();
78
79
        foreach ($this->getHandlers() as $id => $command) {
80
            if ($this->isAllowed($command, $file)) {
0 ignored issues
show
Bug introduced by
It seems like $file defined by parameter $file on line 75 can also be of type array; however, gplcart\modules\file_man...ls\Command::isAllowed() does only seem to accept object<SplFileInfo>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
81
                $commands[$id] = $command;
82
            }
83
        }
84
85
        return $commands;
86
    }
87
88
    /**
89
     * Whether the command is allowed for the file
90
     * @param array $command
91
     * @param SplFileInfo $file
92
     * @return boolean
93
     */
94
    public function isAllowed(array $command, $file)
95
    {
96
        if (!$file instanceof SplFileInfo || empty($command['command_id'])) {
97
            return false;
98
        }
99
100
        $result = $this->callHandler($command, 'allowed', array($file, $command));
101
102
        return is_bool($result) ? $result : false;
103
    }
104
105
    /**
106
     * Call a command handler
107
     * @param array $command
108
     * @param string $method
109
     * @param array $args
110
     * @return mixed
111
     */
112
    public function callHandler(array $command, $method, $args = array())
113
    {
114
        try {
115
            $handlers = $this->getHandlers();
116
            return Handler::call($handlers, $command['command_id'], $method, $args);
117
        } catch (Exception $ex) {
118
            return $ex->getMessage();
119
        }
120
    }
121
122
    /**
123
     * Submit a command
124
     * @param array $command
125
     * @param array $args
126
     * @return array
127
     */
128
    public function submit(array $command, array $args)
129
    {
130
        $result = (array) $this->callHandler($command, 'submit', $args);
131
        $result += array('redirect' => '', 'message' => '', 'severity' => '');
132
        return $result;
133
    }
134
135
    /**
136
     * Returns an array of data used to display the command
137
     * @param array $command
138
     * @param array $args
139
     * @return array
140
     */
141
    public function getView(array $command, array $args)
142
    {
143
        return $this->callHandler($command, 'view', $args);
144
    }
145
146
}
147