Failed Conditions
Push — master ( 516359...a8b39a )
by Florent
04:00
created

ClientRepository::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 94
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 8.4378
c 0
b 0
f 0
cc 1
eloc 72
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Component\Server\Tests\Stub;
15
16
use OAuth2Framework\Component\Server\Model\Client\Client;
17
use OAuth2Framework\Component\Server\Model\Client\ClientId;
18
use OAuth2Framework\Component\Server\Model\Client\ClientRepositoryInterface;
19
use OAuth2Framework\Component\Server\Model\DataBag\DataBag;
20
use OAuth2Framework\Component\Server\Model\Event\EventStoreInterface;
21
use OAuth2Framework\Component\Server\Model\UserAccount\UserAccountId;
22
use SimpleBus\Message\Recorder\RecordsMessages;
23
24
final class ClientRepository implements ClientRepositoryInterface
25
{
26
    /**
27
     * @var EventStoreInterface
28
     */
29
    private $eventStore;
30
31
    /**
32
     * @var RecordsMessages
33
     */
34
    private $eventRecorder;
35
36
    /**
37
     * ClientRepository constructor.
38
     *
39
     * @param EventStoreInterface $eventStore
40
     * @param RecordsMessages     $eventRecorder
41
     */
42
    public function __construct(EventStoreInterface $eventStore, RecordsMessages $eventRecorder)
43
    {
44
        $this->eventStore = $eventStore;
45
        $this->eventRecorder = $eventRecorder;
46
        $this->createAndSaveClient(
47
            ClientId::create('client1'),
48
            DataBag::createFromArray([
49
                'token_endpoint_auth_method' => 'client_secret_basic',
50
                'client_secret' => 'secret',
51
                'grant_types' => ['client_credentials', 'password', 'refresh_token', 'authorization_code', 'urn:ietf:params:oauth:grant-type:jwt-bearer'],
52
                'response_types' => ['code', 'token', 'id_token', 'code token', 'code id_token', 'id_token token', 'code id_token token', 'none'],
53
                'redirect_uris' => ['https://example.com/'],
54
                'id_token_signed_response_alg' => 'ES256',
55
            ]),
56
            UserAccountId::create('User1')
57
        );
58
59
        $this->createAndSaveClient(
60
            ClientId::create('client2'),
61
            DataBag::createFromArray([
62
                'token_endpoint_auth_method' => 'none',
63
                'grant_types' => ['client_credentials', 'authorization_code'],
64
                'userinfo_signed_response_alg' => 'none',
65
            ]),
66
            UserAccountId::create('User1')
67
        );
68
69
        $this->createAndSaveClient(
70
            ClientId::create('client3'),
71
            DataBag::createFromArray([
72
                'token_endpoint_auth_method' => 'client_secret_jwt',
73
                'client_secret' => 'secret',
74
                'client_secret_expires_at' => (new \DateTimeImmutable('now + 1 day'))->getTimestamp(),
75
                'grant_types' => ['client_credentials', 'password', 'refresh_token', 'authorization_code'],
76
            ]),
77
            UserAccountId::create('User1')
78
        );
79
80
        $this->createAndSaveClient(
81
            ClientId::create('client4'),
82
            DataBag::createFromArray([
83
                'token_endpoint_auth_method' => 'client_secret_post',
84
                'client_secret' => 'secret',
85
                'client_secret_expires_at' => (new \DateTimeImmutable('now + 1 day'))->getTimestamp(),
86
            ]),
87
            UserAccountId::create('User1')
88
        );
89
90
        $this->createAndSaveClient(
91
            ClientId::create('79b407fb-acc0-4880-ab98-254062c214ce'),
92
            DataBag::createFromArray([
93
                'registration_access_token' => 'JNWuIxHkTKtUmmtEpipDtPlTc3ordUNpSVVPLbQXKrFKyYVDR7N3k1ZzrHmPWXoibr2J2HrTSSozN6zIhHuypA',
94
                'grant_types' => [],
95
                'response_types' => [],
96
                'redirect_uris' => ['https://www.foo.com'],
97
                'software_statement' => 'eyJhbGciOiJFUzI1NiJ9.eyJzb2Z0d2FyZV92ZXJzaW9uIjoiMS4wIiwic29mdHdhcmVfbmFtZSI6Ik15IGFwcGxpY2F0aW9uIiwic29mdHdhcmVfbmFtZSNlbiI6Ik15IGFwcGxpY2F0aW9uIiwic29mdHdhcmVfbmFtZSNmciI6Ik1vbiBhcHBsaWNhdGlvbiJ9.88m8-YyguCCx1QNChwfNnMZ9APKpNC--nnfB1rVBpAYyHLixtsyMuuI09svqxuiRfTxwgXuRUvsg_5RozmtusQ',
98
                'software_version' => '1.0',
99
                'software_name' => 'My application',
100
                'software_name#en' => 'My application',
101
                'software_name#fr' => 'Mon application',
102
                'registration_client_uri' => 'https://www.config.example.com/client/79b407fb-acc0-4880-ab98-254062c214ce',
103
                'client_id_issued_at' => 1482177703,
104
            ]),
105
            UserAccountId::create('User1')
106
        );
107
108
        $this->createAndSaveClient(
109
            ClientId::create('DISABLED_CLIENT'),
110
            DataBag::createFromArray([
111
                'token_endpoint_auth_method' => 'client_secret_basic',
112
                'client_secret' => 'secret',
113
                'grant_types' => ['client_credentials', 'password', 'refresh_token', 'authorization_code', 'urn:ietf:params:oauth:grant-type:jwt-bearer'],
114
                'response_types' => ['code', 'token', 'id_token', 'code token', 'code id_token', 'id_token token', 'code id_token token', 'none'],
115
                'redirect_uris' => ['https://example.com/'],
116
                'id_token_signed_response_alg' => 'ES256',
117
            ]),
118
            UserAccountId::create('User1'),
119
            true
120
        );
121
122
        $this->createAndSaveClient(
123
            ClientId::create('client5'),
124
            DataBag::createFromArray([
125
                'token_endpoint_auth_method' => 'client_secret_basic',
126
                'client_secret' => 'secret',
127
                'client_secret_expires_at' => time() - 100,
128
                'grant_types' => ['client_credentials', 'password', 'refresh_token', 'authorization_code', 'urn:ietf:params:oauth:grant-type:jwt-bearer'],
129
                'response_types' => ['code', 'token', 'id_token', 'code token', 'code id_token', 'id_token token', 'code id_token token', 'none'],
130
                'redirect_uris' => ['https://example.com/'],
131
                'id_token_signed_response_alg' => 'ES256',
132
            ]),
133
            UserAccountId::create('User1')
134
        );
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function find(ClientId $clientId): ?Client
141
    {
142
        $client = null;
143
        $events = $this->eventStore->getEvents($clientId);
144
        if (!empty($events)) {
145
            $client = Client::createEmpty();
146
            foreach ($events as $event) {
147
                $client = $client->apply($event);
148
            }
149
        }
150
151
        return $client;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function save(Client $client)
158
    {
159
        $events = $client->recordedMessages();
160
        foreach ($events as $event) {
161
            $this->eventStore->save($event);
162
            $this->eventRecorder->record($event);
163
        }
164
        $client->eraseMessages();
165
    }
166
167
    private function createAndSaveClient(ClientId $clientId, DataBag $parameters, UserAccountId $ownerId = null, $markAsDeleted = false)
168
    {
169
        $client = Client::createEmpty();
170
        $client = $client->create(
171
            $clientId,
172
            $parameters,
173
            $ownerId
174
        );
175
        if (true === $markAsDeleted) {
176
            $client = $client->markAsDeleted();
177
        }
178
        $events = $client->recordedMessages();
179
        foreach ($events as $event) {
180
            $this->eventStore->save($event);
181
        }
182
        $client->eraseMessages();
183
        $this->save($client);
184
    }
185
}
186