Completed
Push — develop ( ed1275...cd64a1 )
by Abdelrahman
01:49
created

AbstractController::getEmailVerificationBroker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Http\Controllers;
6
7
use Illuminate\Routing\Controller;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Foundation\Bus\DispatchesJobs;
10
use Illuminate\Foundation\Validation\ValidatesRequests;
11
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
12
13
abstract class AbstractController extends Controller
14
{
15
    use DispatchesJobs;
16
    use ValidatesRequests;
17
    use AuthorizesRequests;
18
19
    /**
20
     * The authentication guard name.
21
     *
22
     * @var string
23
     */
24
    protected $guard;
25
26
    /**
27
     * The password reset broker name.
28
     *
29
     * @var string
30
     */
31
    protected $passwordResetBroker;
32
33
    /**
34
     * The email verification broker name.
35
     *
36
     * @var string
37
     */
38
    protected $emailVerificationBroker;
39
40
    /**
41
     * Whitelisted methods.
42
     * Array of whitelisted methods which do not need to go through middleware.
43
     *
44
     * @var array
45
     */
46
    protected $middlewareWhitelist = [];
47
48
    /**
49
     * Create a new abstract controller instance.
50
     */
51
    public function __construct()
52
    {
53
        // Assign global route parameters
54
        if ($route = request()->route()) {
55
            $accessarea = str_before(Route::currentRouteName(), '.');
56
            $passwordResetBroker = $this->getPasswordResetBroker();
57
            $guard = $this->getGuard();
58
59
            $route->setParameter('passwordResetBroker', $passwordResetBroker);
60
            $route->setParameter('accessarea', $accessarea);
61
            $route->setParameter('guard', $guard);
62
63
            // Activate Guardians
64
            ! in_array($accessarea, config('cortex.auth.guardians')) || $this->middleware('auth.basic:guardians,username');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
65
        }
66
    }
67
68
    /**
69
     * Guess guard from accessarea.
70
     *
71
     * @return string
72
     */
73
    protected function guessGuard(): string
74
    {
75
        $accessarea = str_before(Route::currentRouteName(), '.');
76
        $guard = str_plural(mb_strstr($accessarea, 'area', true));
77
78
        return config('auth.guards.'.$guard) ? $guard : config('auth.defaults.guard');
79
    }
80
81
    /**
82
     * Get the guard to be used during authentication.
83
     *
84
     * @return string
85
     */
86
    protected function getGuard(): string
87
    {
88
        return $this->guard ?? $this->guessGuard();
89
    }
90
91
    /**
92
     * Get the guest middleware for the application.
93
     */
94
    protected function getGuestMiddleware()
95
    {
96
        return ($guard = $this->getGuard()) ? 'guest:'.$guard : 'guest';
97
    }
98
99
    /**
100
     * Get the auth middleware for the application.
101
     *
102
     * @return string
103
     */
104
    protected function getAuthMiddleware(): string
105
    {
106
        return ($guard = $this->getGuard()) ? 'auth:'.$guard : 'auth';
107
    }
108
109
    /**
110
     * Guess password reset broker from accessarea.
111
     *
112
     * @return string
113
     */
114
    protected function guessPasswordResetBroker(): string
115
    {
116
        $accessarea = str_before(Route::currentRouteName(), '.');
117
        $passwordResetBroker = str_plural(mb_strstr($accessarea, 'area', true));
118
119
        return config('auth.passwords.'.$passwordResetBroker) ? $passwordResetBroker : config('auth.defaults.passwords');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
120
    }
121
122
    /**
123
     * Get the password reset broker to be used.
124
     *
125
     * @return string
126
     */
127
    protected function getPasswordResetBroker(): string
128
    {
129
        return $this->passwordResetBroker ?? $this->guessPasswordResetBroker();
130
    }
131
132
    /**
133
     * Guess email verification broker from accessarea.
134
     *
135
     * @return string
136
     */
137
    protected function guessEmailVerificationBroker(): string
138
    {
139
        $accessarea = str_before(Route::currentRouteName(), '.');
140
        $emailVerificationBroker = str_plural(mb_strstr($accessarea, 'area', true));
141
142
        return config('auth.passwords.'.$emailVerificationBroker) ? $emailVerificationBroker : config('auth.defaults.passwords');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
143
    }
144
145
    /**
146
     * Get the email verification broker to be used.
147
     *
148
     * @return string
149
     */
150
    protected function getEmailVerificationBroker(): string
151
    {
152
        return $this->emailVerificationBroker ?? $this->guessEmailVerificationBroker();
153
    }
154
}
155