Test Setup Failed
Branch master (a04785)
by Oskars
07:43 queued 05:28
created

Scenarios::Scenario_Set_From_Current_Url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Scenarios::currentControllerMethod() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SolumDeSignum\Scenarios;
6
7
use function config;
0 ignored issues
show
introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
8
use Illuminate\Support\Facades\Route;
9
use function is_bool;
10
11
trait Scenarios
12
{
13
    /**
14
     * @var string
15
     */
16
    public $scenario;
17
18
    /**
19
     * @var string
20
     */
21
    public $setMethodFromController;
22
23
    /**
24
     * @var string
25
     */
26
    public $setMethodFromUrl;
27
28
    /**
29
     * Create a new rule instance.
30
     *
31
     * Scenarios constructor.
32
     *
33
     * @throws \Exception
34
     */
35
    public function __construct()
36
    {
37
        // Set Config
38
        $this->setMethodFromController = config(
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( 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->setMethodFromController = /** @scrutinizer ignore-call */ config(
Loading history...
39
            'scenarios.features.setMethodFromController',
40
            true
41
        );
42
        $this->setMethodFromUrl = config(
43
            'scenarios.features.setMethodFromUrlSegment',
44
            false
45
        );
46
47
        // Detect package abuse
48
        $this->exceptionOneSetMethod();
49
        $this->exceptionOnlyOneSetMethod();
50
51
        // setMethod based on Controller function
52
        if ($this->setMethodFromController) {
53
            $this->scenario = $this->patternFilter($this->currentControllerMethod());
54
        }
55
56
        // setMethod based on Request segment based on $controllerMethodPattern
57
        if ($this->setMethodFromUrl) {
58
            $this->scenario = $this->patternFilter($this->currentRequestUri());
59
        }
60
    }
61
62
    /**
63
     * @param $method
64
     *
65
     * @throws \Exception
66
     *
67
     * @return string
68
     */
69
    public function patternFilter($method): string
70
    {
71
        preg_match_all(
72
            config('scenarios.methods.pattern'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
            /** @scrutinizer ignore-call */ 
73
            config('scenarios.methods.pattern'),
Loading history...
73
            strtolower($method),
74
            $matches
75
        );
76
77
        $this->exceptionScenarioPattern($matches[0][0]);
78
79
        return $matches[0][0];
80
    }
81
82
    /**
83
     * @return string|null
84
     */
85
    public function currentControllerMethod(): ?string
86
    {
87
        return Route::current() !== null ?
88
            Route::current()->getActionMethod() :
89
            null;
90
    }
91
92
    /**
93
     * @return string|null
94
     */
95
    public function currentRequestUri(): ?string
96
    {
97
        return Route::getCurrentRequest() !== null ?
98
            Route::getCurrentRequest()->getRequestUri() :
99
            null;
100
    }
101
102
    /**
103
     * @param mixed $matches
104
     *
105
     * @throws \Exception
106
     */
107
    private function exceptionScenarioPattern($matches): void
108
    {
109
        if (isset($matches) === false) {
110
            throw new \Exception(
111
                'Scenarios patternFilter failed finding match, check $scenarioPattern , LIKE RIGHT NOW !!!'
112
            );
113
        }
114
    }
115
116
    private function exceptionOneSetMethod(): void
117
    {
118
        if (
119
            is_bool($this->setMethodFromController) === false ||
0 ignored issues
show
introduced by
The condition is_bool($this->setMethodFromController) === false is always true.
Loading history...
120
            is_bool($this->setMethodFromUrl) === false ||
121
            ($this->setMethodFromController === false && $this->setMethodFromUrl === false)
122
        ) {
123
            throw new \Exception(
124
                'Please enable at least one setMethod function, LIKE RIGHT NOW !!!'
125
            );
126
        }
127
    }
128
129
    private function exceptionOnlyOneSetMethod(): void
130
    {
131
        if (
132
            is_bool($this->setMethodFromController) === false ||
0 ignored issues
show
introduced by
The condition is_bool($this->setMethodFromController) === false is always true.
Loading history...
133
            is_bool($this->setMethodFromUrl) === false ||
134
            ($this->setMethodFromController === true && $this->setMethodFromUrl === true)
135
        ) {
136
            throw new \Exception(
137
                'Please enable only one setMethod function, LIKE RIGHT NOW !!!'
138
            );
139
        }
140
    }
141
}
142