Builder::router()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace Bavix\Builder;
4
5
use Bavix\Config\Config;
6
use Bavix\Context\Cookies;
7
use Bavix\Context\Session;
8
use Bavix\Flow\Flow;
9
use Bavix\Http\ServerRequest;
10
use Bavix\Lumper\Bind;
11
use Bavix\Processors\Factory;
12
use Bavix\Router\Router;
13
use Bavix\SDK\Path;
14
use Bavix\Security\Password;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
class Builder
18
{
19
20
    /**
21
     * @var string
22
     */
23
    protected $root;
24
25
    /**
26
     * Builder constructor.
27
     *
28
     * @param string $root
29
     */
30 8
    public function __construct(string $root)
31
    {
32 8
        $this->root = Path::slash($root);
33 8
    }
34
35
    /**
36
     * @return Config
37
     */
38
    public function config(): Config
39
    {
40 7
        return Bind::once(__METHOD__, function () {
41 1
            return new Config($this->root . 'etc');
42 7
        });
43
    }
44
45
    /**
46
     * @return Router
47
     */
48
    public function router(): Router
49
    {
50 1
        return Bind::once(__METHOD__, function () {
51 1
            return new Router($this->config()->get('resolver'));
52 1
        });
53
    }
54
55
    /**
56
     * @return ServerRequestInterface
57
     */
58 1
    private function _request(): ServerRequestInterface
59
    {
60 1
        $scheme = filter_input(INPUT_SERVER, 'REQUEST_SCHEME');
61 1
        $host   = filter_input(INPUT_SERVER, 'HTTP_HOST');
62 1
        $uri    = filter_input(INPUT_SERVER, 'REQUEST_URI');
63
64 1
        $uriObject = $this->factory()->uri
65 1
            ->createUri($scheme . '://' . $host . $uri);
66
67 1
        $request = $this->factory()->request->createServerRequest(
68 1
            filter_input(INPUT_SERVER, 'REQUEST_METHOD') ?? 'GET',
69 1
            $uriObject
70
        );
71
72 1
        $query = filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) ?: [];
73 1
        $data  = filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) ?: [];
74
75
        return $request
76 1
            ->withQueryParams($query)
77 1
            ->withParsedBody($data)
78 1
            ->withUploadedFiles($_FILES);
79
    }
80
81
    /**
82
     * @return ServerRequestInterface
83
     */
84
    public function request(): ServerRequestInterface
85
    {
86 2
        return Bind::once(__METHOD__, function () {
87
88 1
            $factory = $this->factory()->request;
89
90 1
            if (method_exists($factory, 'createServerRequestFromGlobals'))
91
            {
92
                $request = $factory::createServerRequestFromGlobals();
93
            }
94
            else
95
            {
96 1
                $request = $this->_request();
97
            }
98
99 1
            if ($request instanceof ServerRequest)
100
            {
101
                return $request
102
                    ->withCookiesContent($this->cookies())
103
                    ->withSessionContent($this->session())
104
                    ->withRouter($this->router());
105
            }
106
107 1
            return $request;
108 2
        });
109
    }
110
111
    /**
112
     * @return Session
113
     */
114
    public function session(): Session
115
    {
116 1
        return Bind::once(__METHOD__, function () {
117
118 1
            $content = $this->config()->get('content');
119 1
            $slice   = $content->getSlice('session');
120
121 1
            return new Session($slice->getData('password'));
0 ignored issues
show
Bug introduced by
It seems like $slice->getData('password') can also be of type array; however, parameter $password of Bavix\Context\Session::__construct() does only seem to accept null|string, 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

121
            return new Session(/** @scrutinizer ignore-type */ $slice->getData('password'));
Loading history...
122
123 1
        });
124
    }
125
126
    /**
127
     * @return Cookies
128
     */
129
    public function cookies(): Cookies
130
    {
131 1
        return Bind::once(__METHOD__, function () {
132
133 1
            $content = $this->config()->get('content');
134 1
            $slice   = $content->getSlice('cookies');
135
136 1
            return new Cookies($slice->getData('password'));
0 ignored issues
show
Bug introduced by
It seems like $slice->getData('password') can also be of type array; however, parameter $password of Bavix\Context\Cookies::__construct() does only seem to accept null|string, 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

136
            return new Cookies(/** @scrutinizer ignore-type */ $slice->getData('password'));
Loading history...
137
138 1
        });
139
    }
140
141
    /**
142
     * @return Factory
143
     */
144
    public function factory(): Factory
145
    {
146 1
        return Bind::once(__METHOD__, function () {
147 1
            return new Factory($this->config()->get('factory'));
148 1
        });
149
    }
150
151
    /**
152
     * @return Flow
153
     */
154
    public function flow(): Flow
155
    {
156 1
        return Bind::once(__METHOD__, function () {
157 1
            return new Flow(null, $this->config()->get('flow')->asArray());
158 1
        });
159
    }
160
161
    /**
162
     * @return Password
163
     */
164
    public function password(): Password
165
    {
166
        return Bind::once(__METHOD__, function () {
167
168
            $slice = $this->config()->get('password');
169
170
            return new Password(
171
                $slice->getData('algo', PASSWORD_DEFAULT),
0 ignored issues
show
Bug introduced by
It seems like $slice->getData('algo', ...ilder\PASSWORD_DEFAULT) can also be of type array; however, parameter $algo of Bavix\Security\Password::__construct() does only seem to accept string, 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

171
                /** @scrutinizer ignore-type */ $slice->getData('algo', PASSWORD_DEFAULT),
Loading history...
172
                $slice->getData('options')
173
            );
174
        });
175
    }
176
177
}
178