RouteMapFeatures   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 125
rs 10
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
B runRouteBinding() 0 27 8
A getStaticAlias() 0 3 1
A getCallbackAlias() 0 3 1
A findDynamicCallbacks() 0 11 4
A isPathInjected() 0 3 2
A findStaticAliases() 0 29 5
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\Arr;
8
use Ffcms\Core\Helper\Type\Str;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
11
/**
12
 * Trait RouteMapFeatures. Process alias and callback route address bindings
13
 * @package Ffcms\Core\Network\Request
14
 */
15
trait RouteMapFeatures
16
{
17
    // special variable for route aliasing
18
    protected $aliasPathTarget;
19
    // special variable for route callback binding
20
    protected $callbackClass;
21
22
    /**
23
     * Build static and dynamic path aliases for working set
24
     * @return void
25
     */
26
    private function runRouteBinding(): 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
        // try to work with static aliases
34
        if (Any::isArray($routing) && isset($routing['Alias'], $routing['Alias'][env_name])) {
0 ignored issues
show
Bug introduced by
The constant Ffcms\Core\Network\Request\env_name was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
            $pathway = $this->findStaticAliases($routing['Alias'][env_name], $pathway);
36
        }
37
38
        $this->setPathdata(explode('/', trim($pathway, '/')));
0 ignored issues
show
Bug introduced by
It seems like setPathdata() 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
        $this->/** @scrutinizer ignore-call */ 
39
               setPathdata(explode('/', trim($pathway, '/')));
Loading history...
39
40
        // set default controller and action for undefined data
41
        if (!$this->action) {
42
            $this->action = 'Index';
0 ignored issues
show
Bug Best Practice introduced by
The property action does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        }
44
45
        // empty or contains backslashes? set to main
46
        if (!$this->controller || Str::contains('\\', $this->controller)) {
47
            $this->controller = 'Main';
0 ignored issues
show
Bug Best Practice introduced by
The property controller does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
        }
49
50
        // find callback injection in routing configs (calculated in App::run())
51
        if (Any::isArray($routing) && isset($routing['Callback'], $routing['Callback'][env_name])) {
52
            $this->findDynamicCallbacks($routing['Callback'][env_name], $this->controller);
53
        }
54
    }
55
56
    /**
57
     * Prepare static pathway aliasing for routing
58
     * @param array|null $map
59
     * @param string|null $pathway
60
     * @return string|null
61
     */
62
    private function findStaticAliases(?array $map = null, ?string $pathway = null): ?string
63
    {
64
        if (!$map) {
65
            return $pathway;
66
        }
67
68
        // current pathway is found as "old path" (or alias target). Make redirect to new pathway.
69
        if (Arr::in($pathway, $map)) {
70
            // find "new path" as binding uri slug
71
            $binding = array_search($pathway, $map, true);
72
            // build url to redirection
73
            $url = $this->getSchemeAndHttpHost() . $this->getBasePath() . '/';
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

73
            $url = $this->/** @scrutinizer ignore-call */ getSchemeAndHttpHost() . $this->getBasePath() . '/';
Loading history...
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

73
            $url = $this->getSchemeAndHttpHost() . $this->/** @scrutinizer ignore-call */ getBasePath() . '/';
Loading history...
74
            if (App::$Properties->get('multiLanguage')) {
75
                $url .= $this->language . '/';
76
            }
77
            $url .= ltrim($binding, '/');
0 ignored issues
show
Bug introduced by
It seems like $binding can also be of type false; however, parameter $str of ltrim() 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

77
            $url .= ltrim(/** @scrutinizer ignore-type */ $binding, '/');
Loading history...
78
79
            $redirect = new RedirectResponse($url);
80
            $redirect->send();
81
            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...
82
        }
83
84
        // current pathway request is equal to path alias. Set alias to property.
85
        if (array_key_exists($pathway, $map)) {
86
            $pathway = $map[$pathway];
87
            $this->aliasPathTarget = $pathway;
88
        }
89
90
        return $pathway;
91
    }
92
93
    /**
94
     * Prepare dynamic callback data for routing
95
     * @param array|null $map
96
     * @param string|null $controller
97
     * @return void
98
     */
99
    private function findDynamicCallbacks(array $map = null, ?string $controller = null): void
100
    {
101
        if (!$map) {
102
            return;
103
        }
104
105
        // try to find global callback for this controller slug
106
        if (array_key_exists($controller, $map)) {
107
            $class = (string)$map[$controller];
108
            if (!Str::likeEmpty($class)) {
109
                $this->callbackClass = $class;
110
            }
111
        }
112
    }
113
114
115
    /**
116
     * Get callback class alias if exist
117
     * @return null|string
118
     */
119
    public function getCallbackAlias(): ?string
120
    {
121
        return $this->callbackClass;
122
    }
123
124
    /**
125
     * Get static alias of pathway if exist
126
     * @return null|string
127
     */
128
    public function getStaticAlias(): ?string
129
    {
130
        return $this->aliasPathTarget;
131
    }
132
133
    /**
134
     * Check if current request is aliased by dynamic or static rule
135
     * @return bool
136
     */
137
    public function isPathInjected(): bool
138
    {
139
        return $this->callbackClass !== null || $this->aliasPathTarget !== null;
140
    }
141
}
142