Failed Conditions
Push — ng ( 75309d...bc6f2d )
by Florent
08:22
created

ClientRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\Bundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Component\Core\Client\Client;
17
use OAuth2Framework\Component\Core\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
20
use Symfony\Component\Cache\Adapter\AdapterInterface;
21
22
class ClientRepository implements \OAuth2Framework\Component\Core\Client\ClientRepository
23
{
24
    /**
25
     * @var AdapterInterface
26
     */
27
    private $cache;
28
29
    /**
30
     * ClientRepository constructor.
31
     *
32
     * @param AdapterInterface $cache
33
     */
34
    public function __construct(AdapterInterface $cache)
35
    {
36
        $this->cache = $cache;
37
        $this->populateClients();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function find(ClientId $clientId): ? Client
44
    {
45
        $client = $this->getFromCache($clientId);
46
47
        return $client;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function save(Client $client)
54
    {
55
        $client->eraseMessages();
56
        $this->cacheObject($client);
57
    }
58
59
    /**
60
     * @param ClientId $clientId
61
     *
62
     * @return Client|null
63
     */
64
    private function getFromCache(ClientId $clientId): ? Client
65
    {
66
        $itemKey = sprintf('oauth2-client-%s', $clientId->getValue());
67
        $item = $this->cache->getItem($itemKey);
68
        if ($item->isHit()) {
69
            return $item->get();
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     * @param Client $client
77
     */
78
    private function cacheObject(Client $client)
79
    {
80
        $itemKey = sprintf('oauth2-client-%s', $client->getPublicId()->getValue());
81
        $item = $this->cache->getItem($itemKey);
82
        $item->set($client);
83
        $item->tag(['oauth2_server', 'client', $itemKey]);
84
        $this->cache->save($item);
85
    }
86
87
    private function populateClients()
88
    {
89
        $client = Client::createEmpty();
90
        $client = $client->create(
91
            ClientId::create('CLIENT_ID_1'),
92
            DataBag::create([
93
                'token_endpoint_auth_method' => 'none',
94
                'grant_types' => [],
95
            ]),
96
            UserAccountId::create('USER_ACCOUNT_1')
97
        );
98
        $client->eraseMessages();
99
        $this->save($client);
100
101
        $client = Client::createEmpty();
102
        $client = $client->create(
103
            ClientId::create('CLIENT_ID_2'),
104
            DataBag::create([
105
                'token_endpoint_auth_method' => 'none',
106
                'grant_types' => ['client_credentials'],
107
            ]),
108
            UserAccountId::create('USER_ACCOUNT_1')
109
        );
110
        $client->eraseMessages();
111
        $this->save($client);
112
113
        $client = Client::createEmpty();
114
        $client = $client->create(
115
            ClientId::create('CLIENT_ID_3'),
116
            DataBag::create([
117
                'token_endpoint_auth_method' => 'client_secret_basic',
118
                'grant_types' => ['client_credentials'],
119
                'client_secret' => 'client_secret',
120
            ]),
121
            UserAccountId::create('USER_ACCOUNT_1')
122
        );
123
        $client->eraseMessages();
124
        $this->save($client);
125
    }
126
}
127