Issues (7)

src/Scenarios.php (5 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SolumDeSignum\Scenarios;
6
7
use function config;
0 ignored issues
show
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
10
trait Scenarios
11
{
12
    /**
13
     * @var string
14
     */
15
    public $scenario;
16
17
    /**
18
     * @var string
19
     */
20
    public $setMethodFromController;
21
22
    /**
23
     * @var string
24
     */
25
    public $setMethodFromUrl;
26
27
    /**
28
     * Create a new rule instance.
29
     *
30
     * Scenarios constructor.
31
     *
32
     * @throws \Exception
33
     */
34
    public function __construct()
35
    {
36
        // Set Config
37
        $this->setMethodFromController = config(
0 ignored issues
show
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

37
        $this->setMethodFromController = /** @scrutinizer ignore-call */ config(
Loading history...
38
            'scenarios.features.setMethodFromController',
39
            true
40
        );
41
        $this->setMethodFromUrl = config(
42
            'scenarios.features.setMethodFromUrlSegment',
43
            false
44
        );
45
46
        // Detect package abuse
47
        $this->exceptionOneSetMethod();
48
        $this->exceptionOnlyOneSetMethod();
49
50
        // setMethod based on Controller function
51
        if ($this->setMethodFromController) {
52
            $this->scenario = $this->patternFilter($this->currentControllerMethod());
53
        }
54
55
        // setMethod based on Request segment based on $controllerMethodPattern
56
        if ($this->setMethodFromUrl) {
57
            $this->scenario = $this->patternFilter($this->currentRequestUri());
58
        }
59
    }
60
61
    /**
62
     * @param $method
63
     *
64
     * @throws \Exception
65
     *
66
     * @return string
67
     */
68
    public function patternFilter($method): string
69
    {
70
        \preg_match_all(
71
            config('scenarios.methods.pattern'),
0 ignored issues
show
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

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