Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

UserRepository::getUserEntityByUserCredentials()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.0555
c 0
b 0
f 0
cc 9
nc 13
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Repositories;
6
7
use RuntimeException;
8
use Illuminate\Support\Str;
9
use Rinvex\Oauth\Bridge\User;
10
use Illuminate\Contracts\Hashing\Hasher;
11
use League\OAuth2\Server\Entities\ClientEntityInterface;
12
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
13
14
class UserRepository implements UserRepositoryInterface
15
{
16
    /**
17
     * The hasher implementation.
18
     *
19
     * @var \Illuminate\Contracts\Hashing\Hasher
20
     */
21
    protected $hasher;
22
23
    /**
24
     * Create a new repository instance.
25
     *
26
     * @param \Illuminate\Contracts\Hashing\Hasher $hasher
27
     *
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct(Hasher $hasher)
31
    {
32
        $this->hasher = $hasher;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
39
    {
40
        if (is_null($model = config('auth.providers.'.Str::plural($clientEntity->user_type).'.model'))) {
0 ignored issues
show
Bug introduced by
Accessing user_type on the interface League\OAuth2\Server\Ent...s\ClientEntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41
            throw new RuntimeException('Unable to determine authentication model from configuration.');
42
        }
43
44
        if (method_exists($model, 'findAndValidateForOAuth')) {
45
            $user = (new $model())->findAndValidateForOAuth($username, $password);
46
47
            if (! $user) {
48
                return;
49
            }
50
51
            return new User($user->getAuthIdentifier());
52
        }
53
54
        if (method_exists($model, 'findForOAuth')) {
55
            $user = (new $model())->findForOAuth($username);
56
        } else {
57
            $user = (new $model())->where('email', $username)->first();
58
        }
59
60
        if (! $user) {
61
            return;
62
        } elseif (method_exists($user, 'validateForOAuthPasswordGrant')) {
63
            if (! $user->validateForOAuthPasswordGrant($password)) {
64
                return;
65
            }
66
        } elseif (! $this->hasher->check($password, $user->getAuthPassword())) {
67
            return;
68
        }
69
70
        return new User($user->getAuthIdentifier());
71
    }
72
}
73