| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace CustomerGauge\Session; |
||
| 4 | |||
| 5 | use Illuminate\Auth\Events\Authenticated; |
||
| 6 | use Illuminate\Auth\GuardHelpers; |
||
| 7 | use Illuminate\Contracts\Auth\Guard; |
||
| 8 | use Illuminate\Contracts\Events\Dispatcher; |
||
| 9 | |||
| 10 | final class NativeSessionGuard implements Guard |
||
| 11 | { |
||
| 12 | use GuardHelpers; |
||
| 13 | |||
| 14 | private $session; |
||
| 15 | |||
| 16 | private $dispatcher; |
||
| 17 | |||
| 18 | 2 | public function __construct(NativeSessionUserProvider $provider, SessionRetriever $session, Dispatcher $dispatcher) |
|
| 19 | { |
||
| 20 | 2 | $this->provider = $provider; |
|
| 21 | 2 | $this->session = $session; |
|
| 22 | 2 | $this->dispatcher = $dispatcher; |
|
| 23 | } |
||
| 24 | |||
| 25 | 2 | public function user() |
|
| 26 | { |
||
| 27 | 2 | if (! is_null($this->user)) { |
|
| 28 | return $this->user; |
||
| 29 | } |
||
| 30 | |||
| 31 | 2 | $session = $this->session->retrieve(); |
|
| 32 | |||
| 33 | // Laravel implements a Chain of Responsibility on the Authentication process. |
||
| 34 | // If this Guard cannot authenticate, we must return null to give room for |
||
| 35 | // other Guards to attempt to authenticate the current request. |
||
| 36 | 2 | if (! $session) { |
|
|
0 ignored issues
–
show
|
|||
| 37 | 1 | return null; |
|
| 38 | } |
||
| 39 | |||
| 40 | 1 | $user = $this->provider->retrieveByCredentials($session); |
|
| 41 | |||
| 42 | 1 | if ($user) { |
|
| 43 | 1 | $this->dispatcher->dispatch(new Authenticated('php-native-session', $user)); |
|
| 44 | |||
| 45 | 1 | $this->user = $user; |
|
| 46 | } |
||
| 47 | |||
| 48 | 1 | return $this->user; |
|
| 49 | } |
||
| 50 | |||
| 51 | public function validate(array $credentials = []) |
||
| 52 | { |
||
| 53 | return (bool) $this->provider->retrieveByCredentials($_SESSION); |
||
| 54 | } |
||
| 55 | } |
||
| 56 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.