Failed Conditions
Push — master ( cccd95...989cb2 )
by Florent
03:57
created

UserRepository::findOneByUsername()   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
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\SecurityBundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
18
use OAuth2Framework\Component\Core\UserAccount\UserAccountRepository;
19
20
class UserRepository implements UserAccountRepository
21
{
22
    private $usersByUsername = [];
23
24
    private $usersByPublicId = [];
25
26
    public function __construct()
27
    {
28
        foreach ($this->getUsers() as $userInformation) {
29
            $user = new User(
30
                $userInformation['username'],
31
                $userInformation['password'],
32
                $userInformation['salt'],
33
                $userInformation['roles'],
34
                $userInformation['oauth2Passwords'],
35
                $userInformation['public_id'],
36
                $userInformation['last_login_at'],
37
                $userInformation['parameters']
38
            );
39
            $this->usersByUsername[$userInformation['username']] = $user;
40
            $this->usersByPublicId[$userInformation['public_id']->getValue()] = $user;
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function findOneByUsername(string $username): ?UserAccount
48
    {
49
        return array_key_exists($username, $this->usersByUsername) ? $this->usersByUsername[$username] : null;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function find(UserAccountId $publicId): ?UserAccount
56
    {
57
        return array_key_exists($publicId->getValue(), $this->usersByPublicId) ? $this->usersByPublicId[$publicId->getValue()] : null;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    private function getUsers(): array
64
    {
65
        return [
66
            [
67
                'id' => 'john.1',
68
                'public_id' => UserAccountId::create('john.1'),
69
                'username' => 'john.1',
70
                'password' => 'secret',
71
                'salt' => null,
72
                'roles' => ['ROLE_USER'],
73
                'last_login_at' => time() - 100,
74
                'parameters' => [
75
                    'password' => 'doe',
76
                    'user' => 'john',
77
                    'address', [
78
                        'street_address' => '5 rue Sainte Anne',
79
                        'region' => 'Île de France',
80
                        'postal_code' => '75001',
81
                        'locality' => 'Paris',
82
                        'country' => 'France',
83
                    ],
84
                    'name' => 'John Doe',
85
                    'given_name' => 'John',
86
                    'family_name' => 'Doe',
87
                    'middle_name' => 'Jack',
88
                    'nickname' => 'Little John',
89
                    'profile' => 'https://profile.doe.fr/john/',
90
                    'preferred_username' => 'j-d',
91
                    'gender' => 'M',
92
                    'phone_number' => '+0123456789',
93
                    'phone_number_verified' => true,
94
                    'updated_at' => 1485431232,
95
                    'zoneinfo' => 'Europe/Paris',
96
                    'locale' => 'en',
97
                    'picture' => 'https://www.google.com',
98
                    'amr' => ['password' => 'otp'],
99
                    'birthdate' => '1950-01-01',
100
                    'email' => '[email protected]',
101
                    'email_verified' => false,
102
                    'last_login_at' => time() - 100,
103
                    'website' => 'https://john.doe.com',
104
                    'website#fr_fr' => 'https://john.doe.fr',
105
                    'website#fr' => 'https://john.doe.fr',
106
                    'picture#de' => 'https://john.doe.de/picture',
107
                ],
108
                'oauth2Passwords' => ['password.1'],
109
            ],
110
            [
111
                'id' => 'john.2',
112
                'public_id' => UserAccountId::create('john.2'),
113
                'username' => 'john.2',
114
                'password' => 'secret',
115
                'salt' => null,
116
                'roles' => ['ROLE_USER'],
117
                'last_login_at' => time() - 100,
118
                'parameters' => [
119
                    'password' => 'doe',
120
                    'user' => 'john',
121
                ],
122
                'oauth2Passwords' => ['doe'],
123
            ],
124
        ];
125
    }
126
}
127