Failed Conditions
Push — master ( 6d8965...e05915 )
by Florent
03:57
created

initAuthorizationCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 59
rs 9.597
cc 1
eloc 46
nc 1
nop 0

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-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\AuthorizationCodeGrant\AuthorizationCode;
17
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeId;
18
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeRepository as AuthorizationCodeRepositoryInterface;
19
use OAuth2Framework\Component\Core\Client\ClientId;
20
use OAuth2Framework\Component\Core\DataBag\DataBag;
21
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
22
23
class AuthorizationCodeRepository implements AuthorizationCodeRepositoryInterface
24
{
25
    /**
26
     * @var AuthorizationCode[]
27
     */
28
    private $authorizationCodes = [];
29
30
    public function __construct()
31
    {
32
        $this->initAuthorizationCodes();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function find(AuthorizationCodeId $authCodeId): ? AuthorizationCode
39
    {
40
        return array_key_exists($authCodeId->getValue(), $this->authorizationCodes) ? $this->authorizationCodes[$authCodeId->getValue()] : null;
41
    }
42
43
    /**
44
     * @param AuthorizationCode $authCode
45
     */
46
    public function save(AuthorizationCode $authCode): void
47
    {
48
        $this->authorizationCodes[$authCode->getTokenId()->getValue()] = $authCode;
49
    }
50
51
    private function initAuthorizationCodes()
52
    {
53
        $authorizationCode = AuthorizationCode::createEmpty();
54
        $authorizationCode = $authorizationCode->create(
55
            AuthorizationCodeId::create('VALID_AUTHORIZATION_CODE'),
56
            ClientId::create('CLIENT_ID_3'),
57
            UserAccountId::create('john.1'),
58
            [],
59
            'http://localhost/callback',
60
            new \DateTimeImmutable('now +1 day'),
61
            DataBag::create([]),
62
            DataBag::create([]),
63
            null
64
        );
65
        $this->save($authorizationCode);
66
67
        $authorizationCode = AuthorizationCode::createEmpty();
68
        $authorizationCode = $authorizationCode->create(
69
            AuthorizationCodeId::create('VALID_AUTHORIZATION_CODE_FOR_CONFIDENTIAL_CLIENT'),
70
            ClientId::create('CLIENT_ID_5'),
71
            UserAccountId::create('john.1'),
72
            [],
73
            'http://localhost/callback',
74
            new \DateTimeImmutable('now +1 day'),
75
            DataBag::create([]),
76
            DataBag::create([]),
77
            null
78
        );
79
        $this->save($authorizationCode);
80
81
        $authorizationCode = AuthorizationCode::createEmpty();
82
        $authorizationCode = $authorizationCode->create(
83
            AuthorizationCodeId::create('REVOKED_AUTHORIZATION_CODE'),
84
            ClientId::create('CLIENT_ID_3'),
85
            UserAccountId::create('john.1'),
86
            [],
87
            'http://localhost/callback',
88
            new \DateTimeImmutable('now +1 day'),
89
            DataBag::create([]),
90
            DataBag::create([]),
91
            null
92
        );
93
        $authorizationCode = $authorizationCode->markAsRevoked();
94
        $this->save($authorizationCode);
95
96
        $authorizationCode = AuthorizationCode::createEmpty();
97
        $authorizationCode = $authorizationCode->create(
98
            AuthorizationCodeId::create('EXPIRED_AUTHORIZATION_CODE'),
99
            ClientId::create('CLIENT_ID_3'),
100
            UserAccountId::create('john.1'),
101
            [],
102
            'http://localhost/callback',
103
            new \DateTimeImmutable('now -1 day'),
104
            DataBag::create([]),
105
            DataBag::create([]),
106
            null
107
        );
108
        $this->save($authorizationCode);
109
    }
110
}
111