MvcFeatures   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 30
c 3
b 0
f 0
dl 0
loc 103
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 3 1
A getAction() 0 3 1
A setPathdata() 0 17 6
A setController() 0 3 1
B searchRedirect() 0 21 7
A getController() 0 3 1
A setAction() 0 3 1
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...
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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 = array_map(function ($in) {
68
                    return Any::isStr($in) ? urldecode($in) : $in;
69
                }, $pathArray);
70
            }
71
        }
72
    }
73
74
    /**
75
     * Get current controller name
76
     * @return string
77
     */
78
    public function getController(): ?string
79
    {
80
        return $this->controller;
81
    }
82
83
    /**
84
     * Get current controller action() name
85
     * @return string
86
     */
87
    public function getAction(): ?string
88
    {
89
        return $this->action;
90
    }
91
92
    /**
93
     * Set current controller name
94
     * @param string $name
95
     */
96
    public function setController($name): void
97
    {
98
        $this->controller = $name;
99
    }
100
101
    /**
102
     * Set current action value
103
     * @param string $name
104
     */
105
    public function setAction($name): void
106
    {
107
        $this->action = $name;
108
    }
109
110
    /**
111
     * Get arguments from pathway
112
     * @return array
113
     */
114
    public function getArguments(): array
115
    {
116
        return (array)$this->args;
117
    }
118
}
119