Passed
Push — master ( c34495...8adf73 )
by Darío
03:13
created

AbstractController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Test Coverage

Coverage 42.59%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 186
ccs 23
cts 54
cp 0.4259
rs 10
c 0
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setMethod() 0 3 1
A getModule() 0 3 1
A getMethod() 0 3 1
A setModule() 0 3 1
A execute() 0 28 6
A isXmlHttpRequest() 0 7 2
A isPost() 0 6 2
A getClassName() 0 3 1
A getJson() 0 17 3
A isGet() 0 6 2
A getPost() 0 7 3
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
use Drone\Mvc\Exception;
14
15
/**
16
 * AbstractionController class
17
 *
18
 * This class manages the interaction between models and views
19
 */
20
abstract class AbstractController
21
{
22
    /**
23
     * Current module instance
24
     *
25
     * @var AbstractModule
26
     */
27
    private $module;
28
29
    /**
30
     * Current method
31
     *
32
     * @var string
33
     */
34
    private $method = null;
35
36
    /**
37
     * Returns the current module instance
38
     *
39
     * @return AbstractModule
40
     */
41 1
    public function getModule()
42
    {
43 1
        return $this->module;
44
    }
45
46
    /**
47
     * Returns the current method
48
     *
49
     * @return string
50
     */
51
    public function getMethod()
52
    {
53
        return $this->method;
54
    }
55
56
    /**
57
     * Sets module instance
58
     *
59
     * @param AbstractModule $module
60
     *
61
     * @return null
62
     */
63 2
    public function setModule(AbstractModule $module)
64
    {
65 2
        $this->module = $module;
66 2
    }
67
68
    /**
69
     * Sets the method attribute
70
     *
71
     * @param string $method
72
     *
73
     * @return null
74
     */
75 8
    public function setMethod($method)
76
    {
77 8
        $this->method = $method;
78 8
    }
79
80
    /**
81
     * Executes the controller
82
     *
83
     * @return mixed
84
     */
85 5
    public function execute()
86
    {
87 5
        $method = $this->method;
88
89 5
        if (is_null($method)) {
0 ignored issues
show
introduced by
The condition is_null($method) is always false.
Loading history...
90
            # This error is thrown because of 'setMethod' method has not been executed
91
            throw new \LogicException("No method has been setted to execute!");
92
        }
93
94 5
        if (!is_null($this->module) && !$this->module->executionIsAllowed()) {
0 ignored issues
show
Bug introduced by
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...
95 1
            throw new Exception\MethodExecutionNotAllowedException("Method execution is not allowed");
96
        } else {
97 5
            if (method_exists($this, $method)) {
98 4
                $class = __CLASS__;
99
100 4
                $reflection = new \ReflectionMethod($this, $method);
101
102 4
                if (!$reflection->isPublic()) {
103 1
                    throw new Exception\PrivateMethodExecutionException(
104 1
                        "The method '$method' is not public in the control class '$class'"
105
                    );
106
                }
107
108 3
                return $this->$method();
109
            } else {
110 1
                $class = __CLASS__;
111 1
                throw new Exception\MethodNotFoundException(
112 1
                    "The method '$method' doesn't exists in the control class '$class'"
113
                );
114
            }
115
        }
116
    }
117
118
    /**
119
     * Returns the class name
120
     *
121
     * @return string
122
     */
123
    public static function getClassName()
124
    {
125
        return __CLASS__;
126
    }
127
128
    /**
129
     * Returns $_POST contents
130
     *
131
     * @return array
132
     */
133
    public function getPost()
134
    {
135
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)) {
136
            $_POST = json_decode(file_get_contents('php://input'), true);
137
        }
138
139
        return (array) $_POST;
140
    }
141
142
    /**
143
     * Returns json contents
144
     *
145
     * @throws LogicException
146
     *
147
     * @return array
148
     */
149
    public function getJson()
150
    {
151
        if ($_SERVER['REQUEST_METHOD'] != 'JSON') {
152
            throw new \LogicException("Request method is not JSON");
153
        }
154
155
        $input =  file_get_contents('php://input');
156
        $array = explode("&", $input);
157
158
        $result = [];
159
160
        foreach ($array as $value) {
161
            $io = explode("=", $value);
162
            $result[$io[0]] = $io[1];
163
        }
164
165
        return $result;
166
    }
167
168
    /**
169
     * Checks if the current request is XmlHttpRequest (AJAX)
170
     *
171
     * @return boolean
172
     */
173
    public function isXmlHttpRequest()
174
    {
175
        # non standard (HTTP_X_REQUESTED_WITH is not a part of PHP)
176
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
177
            return true;
178
        }
179
        return false;
180
    }
181
182
    /**
183
     * Checks if the current request is POST
184
     *
185
     * @return boolean
186
     */
187
    public function isPost()
188
    {
189
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
190
            return true;
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
        return false;
206
    }
207
}
208