Completed
Push — develop ( d114fd...73c793 )
by Abdelrahman
16:17
created

AbstractController::guessGuard()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 1
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.
21
     *
22
     * @var string
23
     */
24
    protected $guard;
25
26
    /**
27
     * The broker name.
28
     *
29
     * @var string
30
     */
31
    protected $broker;
32
33
    /**
34
     * Whitelisted methods.
35
     * Array of whitelisted methods which do not need to go through middleware.
36
     *
37
     * @var array
38
     */
39
    protected $middlewareWhitelist = [];
40
41
    /**
42
     * Create a new abstract controller instance.
43
     */
44
    public function __construct()
45
    {
46
        // Attach accessarea, guard and broker to request parameters dynamically
47
        request()->request->add(['accessarea' => $accessarea = str_before(Route::currentRouteName(), '.')]);
48
        request()->request->add(['broker' => $this->getBroker() ?? $this->guessBroker($accessarea)]);
49
        request()->request->add(['guard' => $this->getGuard() ?? $this->guessGuard($accessarea)]);
50
51
        // Activate Sentinels
52
        ! in_array($accessarea, config('cortex.fort.sentinels')) || $this->middleware('auth.basic:sentinels');
53
    }
54
55
    /**
56
     * Guess guard from accessarea.
57
     *
58
     * @param string $accessarea
59
     *
60
     * @return string|null
61
     */
62
    protected function guessGuard(string $accessarea): ?string
63
    {
64
        return $this->guard = config('auth.guards.'.$guard = str_plural(strstr($accessarea, 'area', true))) ? $guard : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 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
     * Guess broker from accessarea.
69
     *
70
     * @param string $accessarea
71
     *
72
     * @return string|null
73
     */
74
    protected function guessBroker(string $accessarea): ?string
75
    {
76
        return $this->broker = config('auth.passwords.'.$broker = str_plural(strstr($accessarea, 'area', true))) ? $broker : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
77
    }
78
79
    /**
80
     * Get the broker to be used.
81
     *
82
     * @return string|null
83
     */
84
    protected function getBroker(): ?string
85
    {
86
        return $this->broker;
87
    }
88
89
    /**
90
     * Get the guard to be used during authentication.
91
     *
92
     * @return string|null
93
     */
94
    protected function getGuard(): ?string
95
    {
96
        return $this->guard;
97
    }
98
99
    /**
100
     * Get the guest middleware for the application.
101
     */
102
    protected function getGuestMiddleware()
103
    {
104
        return ($guard = $this->getGuard()) ? 'guest:'.$guard : 'guest';
105
    }
106
107
    /**
108
     * Get the auth middleware for the application.
109
     *
110
     * @return string
111
     */
112
    protected function getAuthMiddleware(): string
113
    {
114
        return ($guard = $this->getGuard()) ? 'auth:'.$guard : 'auth';
115
    }
116
}
117