Completed
Push — develop ( f2fd5d...05febb )
by Abdelrahman
01:23
created

EloquentUserProvider::retrieveByCredentials()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Overrides\Illuminate\Auth;
6
7
use Illuminate\Support\Str;
8
use Illuminate\Auth\EloquentUserProvider as BaseEloquentUserProvider;
9
10
class EloquentUserProvider extends BaseEloquentUserProvider
11
{
12
    /**
13
     * Retrieve a user by their unique identifier.
14
     *
15
     * @param  mixed  $identifier
16
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Database\Elo...erns\BuildsQueries|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
17
     */
18
    public function retrieveById($identifier)
19
    {
20
        $model = $this->createModel();
21
22
        return $model->newQuery()
23
            ->with(['roles' => function ($query) {
24
                $query->withoutGlobalScope('tenantable');
25
            }])
26
            ->where($model->getAuthIdentifierName(), $identifier)
27
            ->first();
28
    }
29
30
    /**
31
     * Retrieve a user by their unique identifier and "remember me" token.
32
     *
33
     * @param  mixed  $identifier
34
     * @param  string  $token
35
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Database\Elo...erns\BuildsQueries|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
36
     */
37
    public function retrieveByToken($identifier, $token)
38
    {
39
        $model = $this->createModel();
40
41
        return $model->newQuery()
42
            ->with(['roles' => function ($query) {
43
                $query->withoutGlobalScope('tenantable');
44
            }])
45
            ->where($model->getAuthIdentifierName(), $identifier)
46
            ->where($model->getRememberTokenName(), $token)
47
            ->first();
48
    }
49
50
    /**
51
     * Retrieve a user by the given credentials.
52
     *
53
     * @param  array  $credentials
54
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\Illuminate\Databas...\Concerns\BuildsQueries?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56
    public function retrieveByCredentials(array $credentials)
57
    {
58
        if (empty($credentials)) {
59
            return;
60
        }
61
62
        // First we will add each credential element to the query as a where clause.
63
        // Then we can execute the query and, if we found a user, return it in a
64
        // Eloquent User "model" that will be utilized by the Guard instances.
65
        $query = $this->createModel()->newQuery()->with(['roles' => function ($query) {
66
            $query->withoutGlobalScope('tenantable');
67
        }]);
68
69
        foreach ($credentials as $key => $value) {
70
            if (! Str::contains($key, 'password')) {
71
                $query->where($key, $value);
72
            }
73
        }
74
75
        return $query->first();
76
    }
77
}
78