UserCredentialProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Xervice\User\Business\Model\Authenticator;
5
6
7
use DataProvider\UserCredentialDataProvider;
8
use DataProvider\UserDataProvider;
9
use Xervice\User\Business\Exception\UserException;
10
use Xervice\User\Persistence\UserDataReader;
11
12
class UserCredentialProvider implements UserCredentialProviderInterface
13
{
14
    /**
15
     * @var \Xervice\User\Persistence\UserDataReader
16
     */
17
    private $userReader;
18
19
    /**
20
     * UserAuthenticator constructor.
21
     *
22
     * @param \Xervice\User\Persistence\UserDataReader $userReader
23
     */
24 1
    public function __construct(UserDataReader $userReader)
25
    {
26 1
        $this->userReader = $userReader;
27 1
    }
28
29
    /**
30
     * @param \DataProvider\UserDataProvider $userDataProvider
31
     * @param string $type
32
     *
33
     * @return \DataProvider\UserCredentialDataProvider
34
     * @throws \Xervice\User\Business\Exception\UserException
35
     */
36
    public function getCredentialsForType(UserDataProvider $userDataProvider, string $type): UserCredentialDataProvider
37
    {
38
        $user = $this->userReader->getUserFromEmail($userDataProvider->getEmail());
39
        if (!$user->hasUserId()) {
40
            $this->throwException(
41
                'User %s not found',
42
                $userDataProvider->getEmail()
43
            );
44
        }
45
46
        return $this->authLogins($user, $type);
47
    }
48
49
    /**
50
     * @param \DataProvider\UserDataProvider $userDataProvider
51
     * @param string $type
52
     *
53
     * @return \DataProvider\UserCredentialDataProvider
54
     * @throws \Xervice\User\Business\Exception\UserException
55
     */
56
    private function authLogins(UserDataProvider $userDataProvider, string $type): UserCredentialDataProvider
57
    {
58
        foreach ($userDataProvider->getUserLogins() as $login) {
59
            if ($login->getType() === $type) {
60
                return $login->getUserCredential();
61
            }
62
        }
63
64
        $this->throwException('No valid login for type %s', $type);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return DataProvider\UserCredentialDataProvider. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
65
    }
66
67
    /**
68
     * @param $message
69
     * @param mixed ...$params
70
     *
71
     * @throws \Xervice\User\Business\Exception\UserException
72
     */
73
    private function throwException($message, ...$params): void
74
    {
75
        throw new UserException(
76
            sprintf(
77
                $message,
78
                ...$params
79
            )
80
        );
81
    }
82
}