Failed Conditions
Push — master ( 547d98...1bf1d9 )
by Florent
08:07
created

initAuthorizationCodes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 35
nc 1
nop 0
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
        $refreshToken = AuthorizationCode::createEmpty();
54
        $refreshToken = $refreshToken->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($refreshToken);
66
67
        $refreshToken = AuthorizationCode::createEmpty();
68
        $refreshToken = $refreshToken->create(
69
            AuthorizationCodeId::create('REVOKED_AUTHORIZATION_CODE'),
70
            ClientId::create('CLIENT_ID_3'),
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
        $refreshToken = $refreshToken->markAsRevoked();
80
        $this->save($refreshToken);
81
82
        $refreshToken = AuthorizationCode::createEmpty();
83
        $refreshToken = $refreshToken->create(
84
            AuthorizationCodeId::create('EXPIRED_AUTHORIZATION_CODE'),
85
            ClientId::create('CLIENT_ID_3'),
86
            UserAccountId::create('john.1'),
87
            [],
88
            'http://localhost/callback',
89
            new \DateTimeImmutable('now -1 day'),
90
            DataBag::create([]),
91
            DataBag::create([]),
92
            null
93
        );
94
        $this->save($refreshToken);
95
    }
96
}
97