Completed
Push — master ( 242e65...f5b54f )
by
unknown
04:49 queued 02:03
created

App::processRequestParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 14
nc 3
nop 1
crap 12
1
<?php
2
declare(strict_types=1);
3
namespace Zewa;
4
5
use Sabre\Event\Emitter;
6
use Zewa\Exception\Exception;
7
use Zewa\HTTP\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Zewa\Request.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
/**
10
 * This class is the starting point for application
11
 *
12
 * @author Zechariah Walden<zech @ zewadesign.com>
13
 */
14
class App
15
{
16
    /**
17
     * Return value from application
18
     *
19
     * @var string
20
     */
21
    private $output = '';
22
23
    /**
24
     * @var Dependency $dependency
25
     */
26
    private $dependency;
27
28
    /**
29
     * @var Emitter
30
     */
31
    private $event;
32
33
    /**
34
     * @var Router
35
     */
36
    private $router;
37
38
    /**
39
     * @var Request
40
     */
41
    private $request;
42
43
    /**
44
     * Application bootstrap process
45
     *
46
     * The application registers the configuration in the app/config/core.php
47
     * and then processes, and makes available the configured resources
48
     *
49
     * App constructor.
50
     * @param Dependency $dependency
51
     */
52
    public function __construct(Dependency $dependency)
53
    {
54
        $this->configuration = $dependency->resolve('\Zewa\Config');
0 ignored issues
show
Bug introduced by
The property configuration does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
        $this->event = $dependency->resolve('\Sabre\Event\Emitter', true);
56
        $this->dependency = $dependency;
57
58
        /** @var Security security */
59
        $this->security = $dependency->resolve('\Zewa\Security', true);
0 ignored issues
show
Bug introduced by
The property security does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
        /** @var Router router */
61
        $this->router = $dependency->resolve('\Zewa\Router', true);
62
        /** @var Request request */
63
        $this->request = $dependency->resolve('\Zewa\HTTP\Request', true);
64
    }
65
66
    /**
67
     * Calls the proper shell for app execution
68
     *
69
     * @access private
70
     */
71
    public function initialize()
72
    {
73
        $request = $this->router->getAction();
74
75
        $isRouteCallback = $this->processRequestParameters($request);
76
        $this->start($isRouteCallback);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param $request array
83
     * @param $request[0] string namespace to load
84
     * @param $request[1] string method to call
85
     * @access private
86
     * @return bool
87
     * @throws Exception
88
     */
89
    private function processRequestParameters($request) : bool
90
    {
91
        $params = $this->router->getParameters();
92
93
        if($request !== null) {
94
            if(is_array($request)) {
95
                $callback = false;
96
                $this->request->setRequest($this->dependency->resolve($request[0]));
97
                $this->request->setMethod(($request[1]??[]));
98
            }  else {
99
                $callback = true;
100
                $this->request->setRequest($request);
101
                array_unshift($params, $this->dependency);
102
            }
103
            $this->request->setParams($params);
104
            return $callback;
105
        }
106
107
        throw new Exception('Invalid request');
108
    }
109
110
    /**
111
     * @param bool $isRouteCallback
112
     */
113
    private function start(bool $isRouteCallback)
114
    {
115
        $request = $this->request->getRequest();
116
        $method = $this->request->getMethod();
117
        $params = $this->request->getParams();
118
119
        if($isRouteCallback === false) { // Routed Callback
120
            $this->output = call_user_func_array(
121
                [&$request, $method],
122
                $params
123
            );
124
        } else {
125
            $this->output = call_user_func_array($request, $params);
126
        }
127
    }
128
129
    /**
130
     * Prepare application return value into a string
131
     *
132
     * @access public
133
     * @return string
134
     */
135
    public function __toString()
136
    {
137
        return $this->output;
138
    }
139
}