GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 17ed34...f4bd34 )
by Omar El
03:18
created

Controller::error()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
3
/**
4
 * The controller class.
5
 *
6
 * The base controller for all other controllers.
7
 * Extend this for each created controller
8
 *
9
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
10
 * @author     Omar El Gabry <[email protected]>
11
 */
12
13
class Controller {
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...
14
15
    /**
16
     * view
17
     *
18
     * @var View
19
     */
20
    protected $view;
21
22
    /**
23
     * request
24
     *
25
     * @var Request
26
     */
27
    public $request;
28
29
    /**
30
     * response
31
     *
32
     * @var Response
33
     */
34
    protected $response;
35
36
    /**
37
     * loaded components
38
     *
39
     * @var array
40
     */
41
    public $components = [];
42
43
    /**
44
     * Constructor
45
     *
46
     * @param Request  $request
47
     * @param Response $response
48
    */
49
    public function __construct(Request $request = null, Response $response = null){
50
51
        $this->request  =  $request  !== null ? $request  : new Request();
52
        $this->response =  $response !== null ? $response : new Response();
53
        $this->view     =  new View($this);
54
55
        // events that that will be triggered for each controller:
56
57
        // 1. load components
58
        $this->initialize();
59
60
        // 2. any logic before calling controller's action(method)
61
        $this->beforeAction();
62
63
        // 3. trigger startup method of loaded components
64
        $this->triggerComponents();
65
    }
66
67
    /**
68
     * triggers component startup methods.
69
     * But, for auth, we are calling authentication and authorization separately
70
     *
71
     * You need to Fire the Components and Controller callbacks in the correct order,
72
     * For example, Authorization depends on form element, so you need to trigger Security first.
73
     *
74
     */
75
    public function triggerComponents(){
76
77
        // You need to Fire the Components and Controller callbacks in the correct orde
78
        // For example, Authorization depends on form element, so you need to trigger Security first.
79
80
        // We supposed to execute startup() method of each component,
81
        // but since we need to call Auth -> authenticate, then Security, Auth -> authorize separately
82
83
        // re-construct components in right order
84
        $components = ['Auth', 'Security'];
85
        foreach($components as $key => $component){
86
            if(!in_array($component, $this->components)){
87
                unset($components[$key]);
88
            }
89
        }
90
91
        foreach($components as $component){
92
93
            if($component === "Auth"){
94
95
                $authenticate = $this->Auth->config("authenticate");
0 ignored issues
show
Documentation introduced by
The property Auth does not exist on object<Controller>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
                if(!empty($authenticate)){
97
                    if(!$this->Auth->authenticate()){
0 ignored issues
show
Documentation introduced by
The property Auth does not exist on object<Controller>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
                        $this->Auth->unauthenticated();
0 ignored issues
show
Documentation introduced by
The property Auth does not exist on object<Controller>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
                    }
100
                }
101
102
                // delay checking authorize till after the loop
103
                $authorize = $this->Auth->config("authorize");
0 ignored issues
show
Documentation introduced by
The property Auth does not exist on object<Controller>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
                continue;
105
106
            }
107
108
            $this->{$component}->startup();
109
        }
110
111
        // authorize
112
        if(!empty($authorize)){
113
            if(!$this->Auth->authorize()){
0 ignored issues
show
Documentation introduced by
The property Auth does not exist on object<Controller>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
114
                $this->Auth->unauthorized();
0 ignored issues
show
Documentation introduced by
The property Auth does not exist on object<Controller>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
            }
116
        }
117
    }
118
119
    /**
120
     * Initialization method.
121
     * initialize components and optionally, assign configuration data
122
     *
123
     */
124
     public function initialize(){
125
126
         $this->loadComponents([
127
             'Auth' => [
128
                     'authenticate' => ['User'],
129
                     'authorize'    => ['Controller']
130
                 ],
131
             'Security'
132
         ]);
133
     }
134
135
    /**
136
     * load the components by setting the component's name to a controller's property.
137
     *
138
     * @param array $components
139
     */
140
    public function loadComponents(array $components){
141
142
        if(!empty($components)){
143
144
            $components = Utility::normalize($components);
145
146
            foreach($components as $component => $config){
147
148
                if(!in_array($component, $this->components, true)){
149
                    $this->components[] = $component;
150
                }
151
152
                $class = $component . "Component";
153
                $this->{$component} = empty($config)? new $class($this): new $class($this, $config);
154
            }
155
        }
156
    }
157
158
    /**
159
     * show error page
160
     *
161
     * call error action method and set response status code
162
     * This will work as well for ajax call, see how ajax calls are handled in main.js
163
     *
164
     * @param int|string $code
165
     *
166
     */
167
    public function error($code){
168
169
        $errors = [
170
            404 => "notfound",
171
            500 => "system",
172
            400 => "badrequest",
173
            401 => "unauthorized",
174
            403 => "forbidden"
175
        ];
176
177
        if(!isset($errors[$code]) || !method_exists("ErrorsController", $errors[$code])){
178
            $code = 500;
179
        }
180
181
        $action = isset($errors[$code])? $errors[$code]: "System";
182
        $this->response->setStatusCode($code);
183
184
        // clear, get page, then send headers
185
        $this->response->clearBuffer();
186
        (new ErrorsController())->{$action}();
187
        $this->response->send();
188
    }
189
190
    /**
191
     * Called before the controller action.
192
     * Used to perform logic that needs to happen before each controller action.
193
     *
194
     */
195
    public function beforeAction(){}
196
197
    /**
198
     * Magic accessor for model autoloading.
199
     *
200
     * @param  string $name Property name
201
     * @return object The model instance
202
     */
203
    public function __get($name) {
204
        return $this->loadModel($name);
205
    }
206
207
    /**
208
     * load model
209
     * It assumes the model's constructor doesn't need parameters for constructor
210
     *
211
     * @param string  $model class name
212
     */
213
    public function loadModel($model){
214
        return $this->{$model} = new $model();
215
    }
216
217
    /**
218
     * forces SSL request
219
     *
220
     * @see core/components/SecurityComponent::secureRequired()
221
     */
222
    public function forceSSL(){
223
        $secured  = "https://" . $this->request->currentUrl();
224
        Redirector::to($secured);
225
    }
226
}
227