Failed Conditions
Push — ng ( 977556...d85ce7 )
by Florent
03:52
created

InitialAccessTokenRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Bundle\Tests\TestBundle\Entity;
15
16
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessToken;
17
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId;
18
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
19
20
class InitialAccessTokenRepository implements \OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenRepository
21
{
22
    /**
23
     * @var InitialAccessToken[]
24
     */
25
    private $initialAccessTokens = [];
26
27
    public function __construct()
28
    {
29
        $this->createInitialAccessTokens();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function find(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken
36
    {
37
        return array_key_exists($initialAccessTokenId->getValue(), $this->initialAccessTokens) ? $this->initialAccessTokens[$initialAccessTokenId->getValue()] : null;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function save(InitialAccessToken $initialAccessToken)
44
    {
45
        $this->initialAccessTokens[$initialAccessToken->getTokenId()->getValue()] = $initialAccessToken;
46
    }
47
48
    private function createInitialAccessTokens()
49
    {
50
        $iat = InitialAccessToken::createEmpty();
51
        $iat = $iat->create(
52
            InitialAccessTokenId::create('VALID_INITIAL_ACCESS_TOKEN_ID'),
53
            UserAccountId::create('john.1'),
54
            new \DateTimeImmutable('now +1 day')
55
        );
56
        $iat->eraseMessages();
57
        $this->save($iat);
58
59
        $iat = InitialAccessToken::createEmpty();
60
        $iat = $iat->create(
61
            InitialAccessTokenId::create('EXPIRED_INITIAL_ACCESS_TOKEN_ID'),
62
            UserAccountId::create('john.1'),
63
            new \DateTimeImmutable('now -1 day')
64
        );
65
        $iat->eraseMessages();
66
        $this->save($iat);
67
    }
68
}
69