Failed Conditions
Push — master ( ed3bfb...d044dc )
by Florent
06:10
created

ClientRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createClientId() 0 3 1
A load() 0 9 2
A save() 0 6 1
A __construct() 0 4 1
A getData() 0 48 1
A find() 0 8 2
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\Repository;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Component\Core\Client\Client as ClientInterface;
18
use OAuth2Framework\Component\Core\Client\ClientId;
19
use OAuth2Framework\Component\Core\Client\ClientRepository as ClientRepositoryInterface;
20
use OAuth2Framework\Component\Core\DataBag\DataBag;
21
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
22
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\Client;
23
use Psr\Cache\CacheItemPoolInterface;
24
25
final class ClientRepository implements ClientRepositoryInterface
26
{
27
    /**
28
     * @var CacheItemPoolInterface
29
     */
30
    private $cache;
31
32
    public function __construct(CacheItemPoolInterface $cache)
33
    {
34
        $this->cache = $cache;
35
        $this->load();
36
    }
37
38
    public function find(ClientId $clientId): ?ClientInterface
39
    {
40
        $item = $this->cache->getItem('Client-'.$clientId->getValue());
41
        if ($item->isHit()) {
42
            return $item->get();
43
        }
44
45
        return null;
46
    }
47
48
    public function save(ClientInterface $client): void
49
    {
50
        Assertion::isInstanceOf($client, Client::class, 'Unsupported client class');
51
        $item = $this->cache->getItem('Client-'.$client->getClientId()->getValue());
52
        $item->set($client);
53
        $this->cache->save($item);
54
    }
55
56
    public function create(ClientId $clientId, DataBag $parameters, ?UserAccountId $ownerId): ClientInterface
57
    {
58
        return new Client($clientId, $parameters, $ownerId);
59
    }
60
61
    public function createClientId(): ClientId
62
    {
63
        return new ClientId(bin2hex(random_bytes(32)));
64
    }
65
66
    private function load(): void
67
    {
68
        foreach ($this->getData() as $datum) {
69
            $client = $this->create(
70
                new ClientId($datum['client_id']),
71
                new DataBag($datum['parameter']),
72
                new UserAccountId($datum['owner_id'])
73
            );
74
            $this->save($client);
75
        }
76
    }
77
78
    private function getData(): array
79
    {
80
        return [
81
            [
82
                'client_id' => 'CLIENT_ID_1',
83
                'owner_id' => 'USER_ACCOUNT_1',
84
                'parameter' => [
85
                    'token_endpoint_auth_method' => 'none',
86
                    'grant_types' => [],
87
                ],
88
            ],
89
            [
90
                'client_id' => 'CLIENT_ID_2',
91
                'owner_id' => 'USER_ACCOUNT_1',
92
                'parameter' => [
93
                    'token_endpoint_auth_method' => 'none',
94
                    'grant_types' => ['client_credentials', 'refresh_token', 'authorization_code', 'password', 'implicit'],
95
                    'response_types' => ['code'],
96
                    'redinect_uris' => [
97
                        'https://exxample.com/cb/?foo=bar',
98
                    ],
99
                ],
100
            ],
101
            [
102
                'client_id' => 'CLIENT_ID_3',
103
                'owner_id' => 'USER_ACCOUNT_1',
104
                'parameter' => [
105
                    'token_endpoint_auth_method' => 'client_secret_post',
106
                    'grant_types' => ['client_credentials', 'refresh_token', 'authorization_code', 'password', 'implicit'],
107
                    'client_secret' => 'secret',
108
                ],
109
            ],
110
            [
111
                'client_id' => 'CLIENT_ID_4',
112
                'owner_id' => 'USER_ACCOUNT_1',
113
                'parameter' => [
114
                    'token_endpoint_auth_method' => 'client_secret_jwt',
115
                    'grant_types' => ['urn:ietf:params:oauth:grant-type:jwt-bearer'],
116
                    'client_secret' => 'secret',
117
                ],
118
            ],
119
            [
120
                'client_id' => 'CLIENT_ID_5',
121
                'owner_id' => 'USER_ACCOUNT_1',
122
                'parameter' => [
123
                    'token_endpoint_auth_method' => 'client_secret_basic',
124
                    'grant_types' => ['client_credentials', 'refresh_token', 'authorization_code', 'password', 'implicit'],
125
                    'client_secret' => 'secret',
126
                ],
127
            ],
128
        ];
129
    }
130
}
131