Passed
Push — master ( 4e648e...f91435 )
by Бабичев
02:32
created

Builder::request()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.4326

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 1
nop 0
dl 0
loc 26
ccs 7
cts 11
cp 0.6364
crap 3.4326
rs 8.8571
c 0
b 0
f 0
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 Psr\Http\Message\ServerRequestInterface;
15
16
class Builder
17
{
18
19
    /**
20
     * @var string
21
     */
22
    protected $root;
23
24
    /**
25
     * Builder constructor.
26
     *
27
     * @param string $root
28
     */
29 8
    public function __construct(string $root)
30
    {
31 8
        $this->root = Path::slash($root);
32 8
    }
33
34
    /**
35
     * @return Config
36
     */
37
    public function config(): Config
38
    {
39 7
        return Bind::once(__METHOD__, function () {
40 1
            return new Config($this->root . 'etc');
41 7
        });
42
    }
43
44
    /**
45
     * @return Router
46
     */
47
    public function router(): Router
48
    {
49 1
        return Bind::once(__METHOD__, function () {
50 1
            return new Router($this->config()->get('resolver'));
51 1
        });
52
    }
53
54
    /**
55
     * @return ServerRequestInterface
56
     */
57 1
    private function _request(): ServerRequestInterface
58
    {
59 1
        $scheme = filter_input(INPUT_SERVER, 'REQUEST_SCHEME');
60 1
        $host   = filter_input(INPUT_SERVER, 'HTTP_HOST');
61 1
        $uri    = filter_input(INPUT_SERVER, 'REQUEST_URI');
62
63 1
        $uriObject = $this->factory()->uri
64 1
            ->createUri($scheme . '://' . $host . $uri);
65
66 1
        $request = $this->factory()->request->createServerRequest(
67 1
            filter_input(INPUT_SERVER, 'REQUEST_METHOD') ?? 'GET',
68 1
            $uriObject
69
        );
70
71 1
        $query = filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) ?: [];
72 1
        $data  = filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) ?: [];
73
74
        return $request
75 1
            ->withQueryParams($query)
76 1
            ->withParsedBody($data)
77 1
            ->withUploadedFiles($_FILES);
78
    }
79
80
    /**
81
     * @return ServerRequestInterface
82
     */
83
    public function request(): ServerRequestInterface
84
    {
85 2
        return Bind::once(__METHOD__, function () {
86
87 1
            $factory = $this->factory()->request;
88
89 1
            if (method_exists($factory, 'createServerRequestFromGlobals'))
90
            {
91
                $request = $factory::createServerRequestFromGlobals();
92
            }
93
            else
94
            {
95 1
                $request = $this->_request();
96
            }
97
98 1
            if ($request instanceof ServerRequest)
99
            {
100
                return $request
0 ignored issues
show
Bug introduced by
The method withCookiesContent() does not seem to exist on object<Bavix\Http\ServerRequest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
                    ->withCookiesContent($this->cookies())
102
                    ->withSessionContent($this->session())
103
                    ->withRouter($this->router());
104
            }
105
106 1
            return $request;
107 2
        });
108
    }
109
110
    /**
111
     * @return Session
112
     */
113 View Code Duplication
    public function session(): Session
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115 1
        return Bind::once(__METHOD__, function () {
116
117 1
            $content = $this->config()->get('content');
118 1
            $slice   = $content->getSlice('session');
119
120 1
            return new Session($slice->getData('password'));
121
122 1
        });
123
    }
124
125
    /**
126
     * @return Cookies
127
     */
128 View Code Duplication
    public function cookies(): Cookies
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 1
        return Bind::once(__METHOD__, function () {
131
132 1
            $content = $this->config()->get('content');
133 1
            $slice   = $content->getSlice('cookies');
134
135 1
            return new Cookies($slice->getData('password'));
136
137 1
        });
138
    }
139
140
    /**
141
     * @return Factory
142
     */
143
    public function factory(): Factory
144
    {
145 1
        return Bind::once(__METHOD__, function () {
146 1
            return new Factory($this->config()->get('factory'));
147 1
        });
148
    }
149
150
    /**
151
     * @return Flow
152
     */
153
    public function flow(): Flow
154
    {
155 1
        return Bind::once(__METHOD__, function () {
156 1
            return new Flow(null, $this->config()->get('flow')->asArray());
157 1
        });
158
    }
159
160
}
161