Failed Conditions
Push — master ( 7c3864...930f9b )
by Florent
14:15
created

UserRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
A findOneByUsername() 0 4 1
A getUsers() 0 12 1
A findUserWithAccount() 0 4 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\User\UserRepository as UserRepositoryInterface;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
18
19
class UserRepository implements UserRepositoryInterface
20
{
21
    /**
22
     * @var User[]
23
     */
24
    private $users = [];
25
    private $accountIds = [];
26
27
    public function __construct()
28
    {
29
        foreach ($this->getUsers() as $data) {
30
            $user = new User(
31
                $data['username'],
32
                $data['roles'],
33
                $data['account_ids'],
34
                $data['last_login_at'],
35
                $data['last_update_at']
36
            );
37
38
            $this->users[$data['username']] = $user;
39
            foreach ($data['account_ids'] as $account_id) {
40
                $this->accountIds[$account_id] = $user;
41
            }
42
        }
43
    }
44
45
    public function findOneByUsername(string $username): ?User
46
    {
47
        return $this->users[$username] ?? null;
48
    }
49
50
    private function getUsers(): array
51
    {
52
        return [
53
            [
54
                'username' => 'admin',
55
                'roles' => ['ROLE_ADMIN', 'ROLE_USER'],
56
                'account_ids' => ['john.1'],
57
                'last_login_at' => new \DateTimeImmutable('now -25 hours'),
58
                'last_update_at' => new \DateTimeImmutable('now -15 days'),
59
            ],
60
        ];
61
    }
62
63
    public function findUserWithAccount(UserAccountId $userAccountId): ?\OAuth2Framework\Component\Core\User\User
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
64
    {
65
        return $this->accountIds[$userAccountId->getValue()] ?? null;
66
    }
67
}
68