Passed
Push — master ( 05d9ca...1aa480 )
by Mihail
03:37
created

MvcFeatures::setAdd()   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 1
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 $args;
19
20
    /**
21
     * Check if current url in redirect map
22
     * @return void
23
     */
24
    private function searchRedirect(): void
25
    {
26
        // calculated depend of language
27
        $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

27
        /** @scrutinizer ignore-call */ 
28
        $pathway = $this->getPathInfo();
Loading history...
28
        /** @var array $routing */
29
        $routing = App::$Properties->getAll('Routing');
30
31
        if (!Any::isArray($routing) || !isset($routing['Redirect']) || !Any::isArray($routing['Redirect'])) {
32
            return;
33
        }
34
35
        // check if source uri is key in redirect target map
36
        if (array_key_exists($pathway, $routing['Redirect'])) {
37
            $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

37
            /** @scrutinizer ignore-call */ 
38
            $target = $this->getSchemeAndHttpHost(); // . $this->getBasePath() . '/' . rtrim($routing['Redirect'][$pathway], '/');
Loading history...
38
            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

38
            if ($this->/** @scrutinizer ignore-call */ getBasePath() !== null && !Str::likeEmpty($this->getBasePath())) {
Loading history...
39
                $target .= '/' . $this->getBasePath();
40
            }
41
            $target .= rtrim($routing['Redirect'][$pathway], '/');
42
            $redirect = new RedirectResponse($target);
43
            $redirect->send();
44
            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...
45
        }
46
    }
47
48
    /**
49
     * Working with path array data
50
     * @param array|null $pathArray
51
     * @return void
52
     */
53
    private function setPathdata(?array $pathArray = null): void
54
    {
55
        if (!Any::isArray($pathArray) || count($pathArray) < 1) {
56
            return;
57
        }
58
59
        // extract controller info from full path array
60
        $this->controller = ucfirst(Str::lowerCase(array_shift($pathArray)));
0 ignored issues
show
Bug introduced by
It seems like $pathArray can also be of type null; however, parameter $array of array_shift() 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

60
        $this->controller = ucfirst(Str::lowerCase(array_shift(/** @scrutinizer ignore-type */ $pathArray)));
Loading history...
61
        if (count($pathArray) > 0) {
62
            // extract action
63
            $this->action = ucfirst(Str::lowerCase(array_shift($pathArray)));
64
65
            // safe other parts to arguments if exist
66
            if (count($pathArray) > 0) {
67
                $this->args = $pathArray;
68
            }
69
        }
70
    }
71
72
    /**
73
     * Get current controller name
74
     * @return string
75
     */
76
    public function getController(): ?string
77
    {
78
        return $this->controller;
79
    }
80
81
    /**
82
     * Get current controller action() name
83
     * @return string
84
     */
85
    public function getAction(): ?string
86
    {
87
        return $this->action;
88
    }
89
90
    /**
91
     * Set current controller name
92
     * @param string $name
93
     */
94
    public function setController($name): void
95
    {
96
        $this->controller = $name;
97
    }
98
99
    /**
100
     * Set current action value
101
     * @param string $name
102
     */
103
    public function setAction($name): void
104
    {
105
        $this->action = $name;
106
    }
107
108
    /**
109
     * Get arguments from pathway
110
     * @return array
111
     */
112
    public function getArguments(): array
113
    {
114
        return (array)$this->args;
115
    }
116
}
117