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

AuthorizationCodeRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 4 2
A save() 0 4 1
A initAuthorizationCodes() 0 59 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\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