Issues (99)

src/Mvc/AbstractController.php (2 issues)

1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
/**
14
 * AbstractionController class
15
 *
16
 * This class manages the interaction between models and views
17
 */
18
abstract class AbstractController
19
{
20
    /**
21
     * Current module instance
22
     *
23
     * @var AbstractModule
24
     */
25
    private $module;
26
27
    /**
28
     * Current method
29
     *
30
     * @var string
31
     */
32
    private $method = null;
33
34
    /**
35
     * Returns the current module instance
36
     *
37
     * @return AbstractModule
38
     */
39 1
    public function getModule()
40
    {
41 1
        return $this->module;
42
    }
43
44
    /**
45
     * Returns the current method
46
     *
47
     * @return string
48
     */
49
    public function getMethod()
50
    {
51
        return $this->method;
52
    }
53
54
    /**
55
     * Sets module instance
56
     *
57
     * @param AbstractModule $module
58
     *
59
     * @return null
60
     */
61 2
    public function setModule(AbstractModule $module)
62
    {
63 2
        $this->module = $module;
64 2
    }
65
66
    /**
67
     * Sets the method attribute
68
     *
69
     * @param string $method
70
     *
71
     * @return null
72
     */
73 8
    public function setMethod($method)
74
    {
75 8
        $this->method = $method;
76 8
    }
77
78
    /**
79
     * Executes the controller
80
     *
81
     * @return mixed
82
     */
83 5
    public function execute()
84
    {
85 5
        $method = $this->method;
86
87 5
        if (is_null($method)) {
0 ignored issues
show
The condition is_null($method) is always false.
Loading history...
88
            # This error is thrown because of 'setMethod' method has not been executed
89
            throw new \LogicException("No method has been setted to execute!");
90
        }
91
92 5
        if (!is_null($this->module) && !$this->module->executionIsAllowed()) {
0 ignored issues
show
Are you sure the usage of $this->module->executionIsAllowed() targeting Drone\Mvc\AbstractModule::executionIsAllowed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93 1
            throw new Exception\MethodExecutionNotAllowedException("Method execution is not allowed");
94
        } else {
95 5
            if (method_exists($this, $method)) {
96 4
                $class = __CLASS__;
97
98 4
                $reflection = new \ReflectionMethod($this, $method);
99
100 4
                if (!$reflection->isPublic()) {
101 1
                    throw new Exception\PrivateMethodExecutionException(
102 1
                        "The method '$method' is not public in the control class '$class'"
103
                    );
104
                }
105
106 3
                return $this->$method();
107
            } else {
108 1
                $class = __CLASS__;
109 1
                throw new Exception\MethodNotFoundException(
110 1
                    "The method '$method' doesn't exists in the control class '$class'"
111
                );
112
            }
113
        }
114
    }
115
116
    /**
117
     * Returns the class name
118
     *
119
     * @return string
120
     */
121
    public static function getClassName()
122
    {
123
        return __CLASS__;
124
    }
125
126
    /**
127
     * Returns $_POST contents
128
     *
129
     * @return array
130
     */
131
    public function getPost()
132
    {
133
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)) {
134
            $_POST = json_decode(file_get_contents('php://input'), true);
135
        }
136
137
        return (array) $_POST;
138
    }
139
140
    /**
141
     * Returns json contents
142
     *
143
     * @throws LogicException
144
     *
145
     * @return array
146
     */
147
    public function getJson()
148
    {
149
        if ($_SERVER['REQUEST_METHOD'] != 'JSON') {
150
            throw new \LogicException("Request method is not JSON");
151
        }
152
153
        $input =  file_get_contents('php://input');
154
        $array = explode("&", $input);
155
156
        $result = [];
157
158
        foreach ($array as $value) {
159
            $io = explode("=", $value);
160
            $result[$io[0]] = $io[1];
161
        }
162
163
        return $result;
164
    }
165
166
    /**
167
     * Checks if the current request is XmlHttpRequest (AJAX)
168
     *
169
     * @return boolean
170
     */
171
    public function isXmlHttpRequest()
172
    {
173
        # non standard (HTTP_X_REQUESTED_WITH is not a part of PHP)
174
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
175
            return true;
176
        }
177
178
        return false;
179
    }
180
181
    /**
182
     * Checks if the current request is POST
183
     *
184
     * @return boolean
185
     */
186
    public function isPost()
187
    {
188
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
189
            return true;
190
        }
191
192
        return false;
193
    }
194
195
    /**
196
     * Checks if the current request is GET
197
     *
198
     * @return boolean
199
     */
200
    public function isGet()
201
    {
202
        if ($_SERVER["REQUEST_METHOD"] == "GET") {
203
            return true;
204
        }
205
206
        return false;
207
    }
208
}
209