App::getMethod()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 11
rs 9.6111
1
<?php
2
/**
3
 * This file is part of product_management
4
 * User: Sinan TURGUT <[email protected]>
5
 * Date: 24.06.2019
6
 * php version 7.2
7
 *
8
 * @category Assessment
9
 * @package  ProductManagement
10
 * @author   Sinan TURGUT <[email protected]>
11
 * @license  See LICENSE file
12
 * @link     https://dev.sinanturgut.com.tr
13
 */
14
15
namespace App\Core;
16
17
use Exception;
18
use ReflectionClass;
19
use ReflectionMethod;
20
use App\Utility\Input;
21
use App\Utility\Redirect;
22
23
/**
24
 * Class App
25
 * @package App\Core
26
 */
27
class App
28
{
29
30
    /**
31
     * @var string
32
     */
33
    private $_class = INDEX_CONTROLLER;
34
    /**
35
     * @var string
36
     */
37
    private $_method = INDEX_ACTION;
38
    /**
39
     * @var array
40
     */
41
    private $_params = [];
42
    /**
43
     * @var string
44
     */
45
    private $_pnf='pageNotFound';
46
47
    /**
48
     * App constructor.
49
     */
50
    public function __construct()
51
    {
52
        $this->parseURL();
53
        try {
54
            $this->getClass();
55
            $this->getMethod();
56
            $this->getParams();
57
        } catch (Exception $ex) {
58
           Redirect::to(404, $this->_pnf, Input::get("url"));
0 ignored issues
show
Bug introduced by
It seems like App\Utility\Input::get('url') can also be of type string; however, parameter $replaceMessage of App\Utility\Redirect::to() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
           Redirect::to(404, $this->_pnf, /** @scrutinizer ignore-type */ Input::get("url"));
Loading history...
59
        }
60
    }
61
62
    /**
63
     *
64
     */
65
    private function getClass()
66
    {
67
68
        if (isset($this->_params[0]) && ! empty($this->_params[0])) {
69
            $this->_class = CONTROLLER_PATH  . $this->_params[0];
70
            unset($this->_params[0]);
71
        }
72
73
        if (!class_exists($this->_class)) {
74
            Redirect::to(404, $this->_pnf, Input::get("url"));
0 ignored issues
show
Bug introduced by
It seems like App\Utility\Input::get('url') can also be of type string; however, parameter $replaceMessage of App\Utility\Redirect::to() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            Redirect::to(404, $this->_pnf, /** @scrutinizer ignore-type */ Input::get("url"));
Loading history...
75
        }
76
        $this->_class = new $this->_class;
0 ignored issues
show
Documentation Bug introduced by
It seems like new $this->_class() of type object is incompatible with the declared type string of property $_class.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
    }
78
79
    /**
80
     * @throws \ReflectionException
81
     */
82
    private function getMethod()
83
    {
84
        if (isset($this->_params[1]) && ! empty($this->_params[1])) {
85
            $this->_method = $this->_params[1];
86
            unset($this->_params[1]);
87
        }
88
        if (!(new ReflectionClass($this->_class))->hasMethod($this->_method)) {
89
            Redirect::to(404, $this->_pnf, Input::get("url"));
0 ignored issues
show
Bug introduced by
It seems like App\Utility\Input::get('url') can also be of type string; however, parameter $replaceMessage of App\Utility\Redirect::to() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            Redirect::to(404, $this->_pnf, /** @scrutinizer ignore-type */ Input::get("url"));
Loading history...
90
        }
91
        if (!(new ReflectionMethod($this->_class, $this->_method))->isPublic()) {
92
            Redirect::to(404, $this->_pnf, Input::get("url"));
93
        }
94
    }
95
96
    /**
97
     *
98
     */
99
    private function getParams()
100
    {
101
        $this->_params = $this->_params ? array_values($this->_params) : [];
102
    }
103
104
    /**
105
     *
106
     */
107
    private function parseURL()
108
    {
109
        if ($url = Input::get("url")) {
110
            $this->_params = explode("/", filter_var(rtrim($url, "/"), FILTER_SANITIZE_URL));
111
            $this->_params=array_slice($this->_params, 1);
112
113
        }
114
    }
115
116
    /**
117
     *
118
     */
119
    public function run()
120
    {
121
        call_user_func_array([$this->_class, $this->_method], $this->_params);
122
    }
123
}
124
125