Passed
Branch master (e985ab)
by Mihail
120:46 queued 116:24
created

Request::getAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ffcms\Core\Network;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Str;
7
use Symfony\Component\HttpFoundation\Request as FoundationRequest;
8
9
/**
10
 * Class Request. Classic implementation of httpfoundation.request with smooth additions and changes which allow
11
 * working as well as in ffcms.
12
 * @package Ffcms\Core\Network
13
 */
14
class Request extends FoundationRequest
15
{
16
    use Request\MvcFeatures, Request\MultiLanguageFeatures, Request\RouteMapFeatures;
17
18
    /**
19
     * Request constructor.
20
     * @inheritdoc
21
     * @param array $query
22
     * @param array $request
23
     * @param array $attributes
24
     * @param array $cookies
25
     * @param array $files
26
     * @param array $server
27
     * @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...
28
     */
29
    public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
30
    {
31
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
32
        $this->searchRedirect();
33
        $this->runMultiLanguage();
34
        $this->runRouteBinding();
35
        $this->loadTrustedProxies();
36
    }
37
38
    /**
39
     * Sets the parameters for this request.
40
     *
41
     * This method also re-initializes all properties.
42
     *
43
     * @param array $query The GET parameters
44
     * @param array $request The POST parameters
45
     * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
46
     * @param array $cookies The COOKIE parameters
47
     * @param array $files The FILES parameters
48
     * @param array $server The SERVER parameters
49
     * @param string $content The raw body data
50
     *
51
     * @api
52
     */
53
    public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
54
    {
55
        parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
56
57
        $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

57
        $basePath = trim(/** @scrutinizer ignore-type */ App::$Properties->get('basePath'), '/');
Loading history...
58
        if ($basePath !== null && Str::length($basePath) > 0) {
59
            $basePath = '/' . $basePath;
60
        }
61
62
        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...
63
            $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...
64
        }
65
66
        // we never try to use path's without friendly url's
67
        $this->basePath = $this->baseUrl = $basePath;
68
    }
69
70
    /**
71
     * Set trusted proxies from configs
72
     */
73
    private function loadTrustedProxies()
74
    {
75
        $proxies = App::$Properties->get('trustedProxy');
76
        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

76
        if ($proxies === null || Str::likeEmpty(/** @scrutinizer ignore-type */ $proxies)) {
Loading history...
77
            return;
78
        }
79
80
        $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

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

142
            $offset = (int)Str::length(/** @scrutinizer ignore-type */ $subDir);
Loading history...
143
            $path = Str::sub($path, --$offset);
144
        }
145
        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...
146
    }
147
}
148