Passed
Push — master ( 03a979...4e648e )
by Бабичев
01:51
created

Builder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 15.49 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
dl 22
loc 142
ccs 46
cts 48
cp 0.9583
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A config() 0 6 1
A router() 0 6 1
A _request() 0 22 3
A request() 0 23 3
A session() 11 11 1
A cookies() 11 11 1
A factory() 0 6 1
A flow() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Lumper\Bind;
10
use Bavix\Processors\Factory;
11
use Bavix\Router\Router;
12
use Bavix\SDK\Path;
13
use Psr\Http\Message\ServerRequestInterface;
14
15
class Builder
16
{
17
18
    /**
19
     * @var string
20
     */
21
    protected $root;
22
23
    /**
24
     * Builder constructor.
25
     *
26
     * @param string $root
27
     */
28 8
    public function __construct(string $root)
29
    {
30 8
        $this->root = Path::slash($root);
31 8
    }
32
33
    /**
34
     * @return Config
35
     */
36
    public function config(): Config
37
    {
38 7
        return Bind::once(__METHOD__, function () {
39 1
            return new Config($this->root . 'etc');
40 7
        });
41
    }
42
43
    /**
44
     * @return Router
45
     */
46
    public function router(): Router
47
    {
48 1
        return Bind::once(__METHOD__, function () {
49 1
            return new Router($this->config()->get('resolver'));
50 1
        });
51
    }
52
53
    /**
54
     * @return ServerRequestInterface
55
     */
56 1
    private function _request(): ServerRequestInterface
57
    {
58 1
        $scheme = filter_input(INPUT_SERVER, 'REQUEST_SCHEME');
59 1
        $host   = filter_input(INPUT_SERVER, 'HTTP_HOST');
60 1
        $uri    = filter_input(INPUT_SERVER, 'REQUEST_URI');
61
62 1
        $uriObject = $this->factory()->uri
63 1
            ->createUri($scheme . '://' . $host . $uri);
64
65 1
        $request = $this->factory()->request->createServerRequest(
66 1
            filter_input(INPUT_SERVER, 'REQUEST_METHOD') ?? 'GET',
67 1
            $uriObject
68
        );
69
70 1
        $query = filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) ?: [];
71 1
        $data  = filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) ?: [];
72
73
        return $request
74 1
            ->withQueryParams($query)
75 1
            ->withParsedBody($data)
76 1
            ->withUploadedFiles($_FILES);
77
    }
78
79
    /**
80
     * @return ServerRequestInterface
81
     */
82
    public function request(): ServerRequestInterface
83
    {
84 2
        return Bind::once(__METHOD__, function () {
85
86 1
            $factory = $this->factory()->request;
87
88 1
            if (method_exists($factory, 'createServerRequestFromGlobals'))
89
            {
90
                $request = $factory::createServerRequestFromGlobals();
91
            }
92
            else
93
            {
94 1
                $request = $this->_request();
95
            }
96
97 1
            if (method_exists($request, 'withRouter'))
98
            {
99
                return $request->withRouter($this->router());
100
            }
101
102 1
            return $request;
103 2
        });
104
    }
105
106
    /**
107
     * @return Session
108
     */
109 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...
110
    {
111 1
        return Bind::once(__METHOD__, function () {
112
113 1
            $content = $this->config()->get('content');
114 1
            $slice   = $content->getSlice('session');
115
116 1
            return new Session($slice->getData('password'));
117
118 1
        });
119
    }
120
121
    /**
122
     * @return Cookies
123
     */
124 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...
125
    {
126 1
        return Bind::once(__METHOD__, function () {
127
128 1
            $content = $this->config()->get('content');
129 1
            $slice   = $content->getSlice('cookies');
130
131 1
            return new Cookies($slice->getData('password'));
132
133 1
        });
134
    }
135
136
    /**
137
     * @return Factory
138
     */
139
    public function factory(): Factory
140
    {
141 1
        return Bind::once(__METHOD__, function () {
142 1
            return new Factory($this->config()->get('factory'));
143 1
        });
144
    }
145
146
    /**
147
     * @return Flow
148
     */
149
    public function flow(): Flow
150
    {
151 1
        return Bind::once(__METHOD__, function () {
152 1
            return new Flow(null, $this->config()->get('flow')->asArray());
153 1
        });
154
    }
155
156
}
157