Passed
Push — master ( 359aba...1f5c21 )
by Mihail
03:12
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 7
1
<?php
2
3
namespace Ffcms\Core\Network;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Str;
7
use Ffcms\Templex\Url\UrlRepository;
0 ignored issues
show
Bug introduced by
The type Ffcms\Templex\Url\UrlRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpFoundation\Request as FoundationRequest;
9
10
/**
11
 * Class Request. Classic implementation of httpfoundation.request with smooth additions and changes which allow
12
 * working as well as in ffcms.
13
 * @package Ffcms\Core\Network
14
 */
15
class Request extends FoundationRequest
16
{
17
    use Request\MvcFeatures, Request\MultiLanguageFeatures, Request\RouteMapFeatures;
18
19
    /**
20
     * Request constructor.
21
     * @inheritdoc
22
     * @param array $query
23
     * @param array $request
24
     * @param array $attributes
25
     * @param array $cookies
26
     * @param array $files
27
     * @param array $server
28
     * @param null $content
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $content is correct as it would always require null to be passed?
Loading history...
29
     */
30
    public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
31
    {
32
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
33
        $this->searchRedirect();
34
        $this->runMultiLanguage();
35
        $this->runRouteBinding();
36
        $this->loadTrustedProxies();
37
        $this->setTemplexFeatures();
38
    }
39
40
    /**
41
     * Sets the parameters for this request.
42
     *
43
     * This method also re-initializes all properties.
44
     *
45
     * @param array $query The GET parameters
46
     * @param array $request The POST parameters
47
     * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
48
     * @param array $cookies The COOKIE parameters
49
     * @param array $files The FILES parameters
50
     * @param array $server The SERVER parameters
51
     * @param string $content The raw body data
52
     *
53
     * @api
54
     */
55
    public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
56
    {
57
        parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
58
59
        $basePath = trim(App::$Properties->get('basePath'), '/');
0 ignored issues
show
Bug introduced by
It seems like Ffcms\Core\App::Properties->get('basePath') can also be of type false; however, parameter $str of trim() 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

59
        $basePath = trim(/** @scrutinizer ignore-type */ App::$Properties->get('basePath'), '/');
Loading history...
60
        if ($basePath !== null && Str::length($basePath) > 0) {
61
            $basePath = '/' . $basePath;
62
        }
63
64
        if (!defined('env_no_uri') || env_no_uri === false) {
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Network\env_no_uri was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
            $basePath .= '/' . strtolower(env_name);
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Network\env_name was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
        }
67
68
        // we never try to use path's without friendly url's
69
        $this->basePath = $this->baseUrl = $basePath;
70
    }
71
72
    /**
73
     * Set trusted proxies from configs
74
     */
75
    private function loadTrustedProxies()
76
    {
77
        $proxies = App::$Properties->get('trustedProxy');
78
        if ($proxies === null || Str::likeEmpty($proxies)) {
0 ignored issues
show
Bug introduced by
It seems like $proxies can also be of type false; however, parameter $string of Ffcms\Core\Helper\Type\Str::likeEmpty() 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

78
        if ($proxies === null || Str::likeEmpty(/** @scrutinizer ignore-type */ $proxies)) {
Loading history...
79
            return;
80
        }
81
82
        $pList = explode(',', $proxies);
0 ignored issues
show
Bug introduced by
It seems like $proxies can also be of type false; however, parameter $string of explode() 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

82
        $pList = explode(',', /** @scrutinizer ignore-type */ $proxies);
Loading history...
83
        $resultList = [];
84
        foreach ($pList as $proxy) {
85
            $resultList[] = trim($proxy);
86
        }
87
        self::setTrustedProxies($resultList);
88
    }
89
90
    /**
91
     * Get pathway as string
92
     * @return string
93
     */
94
    public function getPathInfo()
95
    {
96
        $route = $this->languageInPath ? Str::sub(parent::getPathInfo(), Str::length($this->language) + 1) : parent::getPathInfo();
97
        if (!Str::startsWith('/', $route)) {
98
            $route = '/' . $route;
99
        }
100
        return $route;
101
    }
102
103
    /**
104
     * Get pathway without current controller/action path
105
     * @return string
106
     */
107
    public function getPathWithoutControllerAction(): ?string
108
    {
109
        $path = trim($this->getPathInfo(), '/');
110
        if ($this->aliasPathTarget !== null) {
111
            $path = trim($this->aliasPathTarget, '/');
112
        }
113
114
        $pathArray = explode('/', $path);
115
        if ($pathArray[0] === Str::lowerCase($this->getController())) {
116
            // unset controller
117
            array_shift($pathArray);
118
            if ($pathArray[0] === Str::lowerCase($this->getAction())) {
119
                // unset action
120
                array_shift($pathArray);
121
            }
122
        }
123
        return trim(implode('/', $pathArray), '/');
124
    }
125
126
    /**
127
     * Get current full request URI
128
     * @return string
129
     */
130
    public function getFullUrl(): string
131
    {
132
        return $this->getSchemeAndHttpHost() . $this->getRequestUri();
133
    }
134
135
    /**
136
     * Get base path from current environment without basePath of subdirectories
137
     * @return string
138
     */
139
    public function getInterfaceSlug(): string
140
    {
141
        $path = $this->getBasePath();
142
        $subDir = App::$Properties->get('basePath');
143
        if ($subDir !== '/') {
144
            $offset = (int)Str::length($subDir);
0 ignored issues
show
Bug introduced by
It seems like $subDir can also be of type false; however, parameter $string of Ffcms\Core\Helper\Type\Str::length() 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

144
            $offset = (int)Str::length(/** @scrutinizer ignore-type */ $subDir);
Loading history...
145
            $path = Str::sub($path, --$offset);
146
        }
147
        return $path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $path could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
148
    }
149
150
    /**
151
     * Set templex template engine URL features
152
     * @return void
153
     */
154
    private function setTemplexFeatures(): void
155
    {
156
        $url = $this->getSchemeAndHttpHost();
157
        $sub = null;
158
        if ($this->getInterfaceSlug() && Str::length($this->getInterfaceSlug()) > 0) {
159
            $sub = $this->getInterfaceSlug() . '/';
160
        }
161
162
        if ($this->languageInPath()) {
163
            $sub .= $this->getLanguage();
164
        }
165
166
        UrlRepository::factory($this->getFullUrl())->setUrlAndSubdir($url, $sub);
167
    }
168
}
169