Failed Conditions
Push — master ( 1bae70...6a9de1 )
by Florent
40:14
created

UserAccountRepository::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 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\ServerBundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Component\Core\UserAccount\UserAccount as BaseUserAccount;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
18
use OAuth2Framework\Component\Core\UserAccount\UserAccountRepository as UserAccountRepositoryInterface;
19
20
final class UserAccountRepository implements UserAccountRepositoryInterface
21
{
22
    /**
23
     * @var UserAccount[]
24
     */
25
    private $userAccounts = [];
26
27
    /**
28
     * @var UserAccount[]
29
     */
30
    private $usernames = [];
31
32
    public function __construct()
33
    {
34
        foreach ($this->getUsers() as $data) {
35
            $userAccount = new UserAccount(
36
                new UserAccountId($data['id']),
37
                $data['username'],
38
                $data['roles'],
39
                $data['last_login_at'],
40
                $data['last_update_at'],
41
                $data['parameters']
42
            );
43
            $this->userAccounts[$data['id']] = $userAccount;
44
            $this->usernames[$data['username']] = $userAccount;
45
        }
46
    }
47
48
    public function find(UserAccountId $publicId): ?BaseUserAccount
49
    {
50
        return \array_key_exists($publicId->getValue(), $this->userAccounts) ? $this->userAccounts[$publicId->getValue()] : null;
51
    }
52
53
    public function findOneByUsername(string $username): ?BaseUserAccount
54
    {
55
        return \array_key_exists($username, $this->usernames) ? $this->usernames[$username] : null;
56
    }
57
58
    private function getUsers(): array
59
    {
60
        return [
61
            [
62
                'username' => 'admin',
63
                'roles' => ['ROLE_ADMIN', 'ROLE_USER'],
64
                'last_login_at' => new \DateTimeImmutable('now -25 hours'),
65
                'last_update_at' => new \DateTimeImmutable('now -15 days'),
66
                'id' => 'john.1',
67
                'parameters' => [
68
                    'address', [
69
                        'street_address' => '5 rue Sainte Anne',
70
                        'region' => 'Île de France',
71
                        'postal_code' => '75001',
72
                        'locality' => 'Paris',
73
                        'country' => 'France',
74
                    ],
75
                    'name' => 'John Doe',
76
                    'given_name' => 'John',
77
                    'family_name' => 'Doe',
78
                    'middle_name' => 'Jack',
79
                    'nickname' => 'Little John',
80
                    'profile' => 'https://profile.doe.fr/john/',
81
                    'preferred_username' => 'j-d',
82
                    'gender' => 'M',
83
                    'phone_number' => '+0123456789',
84
                    'phone_number_verified' => true,
85
                    'zoneinfo' => 'Europe/Paris',
86
                    'locale' => 'en',
87
                    'picture' => 'https://www.google.com',
88
                    'birthdate' => '1950-01-01',
89
                    'email' => '[email protected]',
90
                    'email_verified' => false,
91
                    'website' => 'https://john.doe.com',
92
                    'website#fr_fr' => 'https://john.doe.fr',
93
                    'website#fr' => 'https://john.doe.fr',
94
                    'picture#de' => 'https://john.doe.de/picture',
95
                ],
96
            ],
97
        ];
98
    }
99
}
100