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.

Controller::startupProcess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
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
    public $response;
35
36
    /**
37
     * redirector
38
     *
39
     * @var Redirector
40
     */
41
    public $redirector;
42
43
    /**
44
     * loaded components
45
     *
46
     * @var array
47
     */
48
    public $components = [];
49
50
    /**
51
     * Constructor
52
     *
53
     * @param Request  $request
54
     * @param Response $response
55
    */
56
    public function __construct(Request $request = null, Response $response = null){
57
58
        $this->request      =  $request  !== null ? $request  : new Request();
59
        $this->response     =  $response !== null ? $response : new Response();
60
        $this->view         =  new View($this);
61
        $this->redirector   =  new Redirector();
62
    }
63
64
    /**
65
     * Perform the startup process for this controller.
66
     * Events that that will be triggered for each controller:
67
     * 1. load components
68
     * 2. perform any logic before calling controller's action(method)
69
     * 3. trigger startup method of loaded components
70
     * 
71
     * @return void|Response
72
     */
73
    public function startupProcess(){
74
75
        $this->initialize();
76
77
        $this->beforeAction();
78
79
        $result = $this->triggerComponents();
80
        if($result instanceof Response){
81
            return $result;
82
        }
83
    }
84
85
    /**
86
     * Initialization method.
87
     * initialize components and optionally, assign configuration data
88
     *
89
     */
90
     public function initialize(){
91
92
         $this->loadComponents([
93
             'Auth' => [
94
                     'authenticate' => ['User'],
95
                     'authorize'    => ['Controller']
96
                 ],
97
             'Security'
98
         ]);
99
     }
100
101
    /**
102
     * load the components by setting the component's name to a controller's property.
103
     *
104
     * @param array $components
105
     */
106
    public function loadComponents(array $components){
107
108
        if(!empty($components)){
109
110
            $components = Utility::normalize($components);
111
112
            foreach($components as $component => $config){
113
114
                if(!in_array($component, $this->components, true)){
115
                    $this->components[] = $component;
116
                }
117
118
                $class = $component . "Component";
119
                $this->{$component} = empty($config)? new $class($this): new $class($this, $config);
120
            }
121
        }
122
    }
123
    
124
    /**
125
     * triggers component startup methods.
126
     * But, for auth, we are calling authentication and authorization separately
127
     *
128
     * You need to Fire the Components and Controller callbacks in the correct order,
129
     * For example, Authorization depends on form element, so you need to trigger Security first.
130
     *
131
     */
132
    private function triggerComponents(){
133
134
        // You need to Fire the Components and Controller callbacks in the correct orde
135
        // For example, Authorization depends on form element, so you need to trigger Security first.
136
137
        // We supposed to execute startup() method of each component,
138
        // but since we need to call Auth -> authenticate, then Security, Auth -> authorize separately
139
140
        // re-construct components in right order
141
        $components = ['Auth', 'Security'];
142
        foreach($components as $key => $component){
143
            if(!in_array($component, $this->components)){
144
                unset($components[$key]);
145
            }
146
        }
147
148
        $result = null;
149
        foreach($components as $component){
150
151
            if($component === "Auth"){
152
153
                $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...
154
                if(!empty($authenticate)){
155
                    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...
156
                        $result = $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...
157
                    }
158
                }
159
160
                // delay checking authorize till after the loop
161
                $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...
162
163
            }else{
164
                $result = $this->{$component}->startup();
165
            }
166
167
            if($result instanceof Response){ return $result; }
168
        }
169
170
        // authorize
171
        if(!empty($authorize)){
172
            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...
173
                $result = $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...
174
            }
175
        }
176
177
        return $result;
178
    }
179
180
    /**
181
     * show error page
182
     *
183
     * call error action method and set response status code
184
     * This will work as well for ajax call, see how ajax calls are handled in main.js
185
     *
186
     * @param int|string $code
187
     *
188
     */
189
    public function error($code){
190
191
        $errors = [
192
            404 => "notfound",
193
            401 => "unauthenticated",
194
            403 => "unauthorized",
195
            400 => "badrequest",
196
            500 => "system"
197
        ];
198
199
        if(!isset($errors[$code]) || !method_exists("ErrorsController", $errors[$code])){
200
            $code = 500;
201
        }
202
203
        $action = isset($errors[$code])? $errors[$code]: "System";
204
        $this->response->setStatusCode($code);
205
206
        // clear, get page, then send headers
207
        $this->response->clearBuffer();
208
        (new ErrorsController($this->request, $this->response))->{$action}();
209
        
210
        return $this->response;
211
    }
212
213
    /**
214
     * Called before the controller action.
215
     * Used to perform logic that needs to happen before each controller action.
216
     *
217
     */
218
    public function beforeAction(){}
219
220
    /**
221
     * Magic accessor for model autoloading.
222
     *
223
     * @param  string $name Property name
224
     * @return object The model instance
225
     */
226
    public function __get($name) {
227
        return $this->loadModel($name);
228
    }
229
230
    /**
231
     * load model
232
     * It assumes the model's constructor doesn't need parameters for constructor
233
     *
234
     * @param string  $model class name
235
     */
236
    public function loadModel($model){
237
        $uc_model = ucwords($model);
238
        return $this->{$model} = new $uc_model();
239
    }
240
241
    /**
242
     * forces SSL request
243
     *
244
     * @see core/components/SecurityComponent::secureRequired()
245
     */
246
    public function forceSSL(){
247
        $secured  = "https://" . $this->request->fullUrlWithoutProtocol();
248
        return $this->redirector->to($secured);
249
    }
250
}
251