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

MvcFeatures::getAdd()   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\Request;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Any;
7
use Ffcms\Core\Helper\Type\Str;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
10
/**
11
 * Trait MvcFeatures. Mvc features for Request networking
12
 * @package Ffcms\Core\Network\Request
13
 */
14
trait MvcFeatures
15
{
16
    protected $controller;
17
    protected $action;
18
    protected $argumentId;
19
    protected $argumentAdd;
20
21
22
    /**
23
     * Check if current url in redirect map
24
     * @return void
25
     */
26
    private function searchRedirect(): void
27
    {
28
        // calculated depend of language
29
        $pathway = $this->getPathInfo();
0 ignored issues
show
Bug introduced by
It seems like getPathInfo() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        /** @scrutinizer ignore-call */ 
30
        $pathway = $this->getPathInfo();
Loading history...
30
        /** @var array $routing */
31
        $routing = App::$Properties->getAll('Routing');
32
33
        if (!Any::isArray($routing) || !isset($routing['Redirect']) || !Any::isArray($routing['Redirect'])) {
34
            return;
35
        }
36
37
        // check if source uri is key in redirect target map
38
        if (array_key_exists($pathway, $routing['Redirect'])) {
39
            $target = $this->getSchemeAndHttpHost(); // . $this->getBasePath() . '/' . rtrim($routing['Redirect'][$pathway], '/');
0 ignored issues
show
Bug introduced by
It seems like getSchemeAndHttpHost() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            /** @scrutinizer ignore-call */ 
40
            $target = $this->getSchemeAndHttpHost(); // . $this->getBasePath() . '/' . rtrim($routing['Redirect'][$pathway], '/');
Loading history...
40
            if ($this->getBasePath() !== null && !Str::likeEmpty($this->getBasePath())) {
0 ignored issues
show
Bug introduced by
It seems like getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            if ($this->/** @scrutinizer ignore-call */ getBasePath() !== null && !Str::likeEmpty($this->getBasePath())) {
Loading history...
41
                $target .= '/' . $this->getBasePath();
42
            }
43
            $target .= rtrim($routing['Redirect'][$pathway], '/');
44
            $redirect = new RedirectResponse($target);
45
            $redirect->send();
46
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
        }
48
    }
49
50
    /**
51
     * Working with path array data
52
     * @param array|null $pathArray
53
     * @return void
54
     */
55
    private function setPathdata(?array $pathArray = null): void
56
    {
57
        if (!Any::isArray($pathArray) || count($pathArray) < 1) {
58
            return;
59
        }
60
61
        // check if array length is more then 4 basic elements and slice it recursive
62
        if (count($pathArray) > 4) {
63
            $this->setPathdata(array_slice($pathArray, 0, 4));
0 ignored issues
show
Bug introduced by
It seems like $pathArray can also be of type null; however, parameter $array of array_slice() does only seem to accept array, 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

63
            $this->setPathdata(array_slice(/** @scrutinizer ignore-type */ $pathArray, 0, 4));
Loading history...
64
            return;
65
        }
66
67
        // Switch path array as reverse without break point! Caution: drugs inside!
68
        switch (count($pathArray)) {
69
            case 4:
70
                $this->argumentAdd = $pathArray[3];
71
            // no break
72
            case 3:
73
                $this->argumentId = $pathArray[2];
74
            // no break
75
            case 2:
76
                $this->action = ucfirst(Str::lowerCase($pathArray[1]));
77
            // no break
78
            case 1:
79
                $this->controller = ucfirst(Str::lowerCase($pathArray[0]));
80
                break;
81
        }
82
    }
83
84
    /**
85
     * Get current controller name
86
     * @return string
87
     */
88
    public function getController(): ?string
89
    {
90
        return $this->controller;
91
    }
92
93
    /**
94
     * Get current controller action() name
95
     * @return string
96
     */
97
    public function getAction(): ?string
98
    {
99
        return $this->action;
100
    }
101
102
    /**
103
     * Get current $id argument for controller action
104
     * @return string|null
105
     */
106
    public function getID(): ?string
107
    {
108
        return urldecode($this->argumentId);
109
    }
110
111
    /**
112
     * Set current controller name
113
     * @param string $name
114
     */
115
    public function setController($name): void
116
    {
117
        $this->controller = $name;
118
    }
119
120
    /**
121
     * Set current action value
122
     * @param string $name
123
     */
124
    public function setAction($name): void
125
    {
126
        $this->action = $name;
127
    }
128
129
    /**
130
     * Set current id argument value
131
     * @param mixed $name
132
     */
133
    public function setId($name): void
134
    {
135
        $this->argumentId = $name;
136
    }
137
138
    /**
139
     * Set current add argument value
140
     * @param mixed $name
141
     */
142
    public function setAdd($name): void
143
    {
144
        $this->argumentAdd = $name;
145
    }
146
147
    /**
148
     * Get current $add argument for controller action
149
     * @return string|null
150
     */
151
    public function getAdd(): ?string
152
    {
153
        return urldecode($this->argumentAdd);
154
    }
155
}
156