Failed Conditions
Push — ng ( 14a26e...d8e250 )
by Florent
03:43
created

AccessTokenRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 4 2
A save() 0 4 1
A create() 0 10 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\Bundle\Tests\TestBundle\Entity;
15
16
use Base64Url\Base64Url;
17
use OAuth2Framework\Component\Core\AccessToken\AccessToken;
18
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId;
19
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository as AccessTokenRepositoryInterface;
20
use OAuth2Framework\Component\Core\Client\ClientId;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
23
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
24
25
class AccessTokenRepository implements AccessTokenRepositoryInterface
26
{
27
    /**
28
     * @var AccessToken[]
29
     */
30
    private $accessTokens = [];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function find(AccessTokenId $accessTokenId)
36
    {
37
        return array_key_exists($accessTokenId->getValue(), $this->accessTokens) ? $this->accessTokens[$accessTokenId->getValue()] : null;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function save(AccessToken $accessToken)
44
    {
45
        $this->accessTokens[$accessToken->getTokenId()->getValue()] = $accessToken;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function create(ResourceOwnerId $resourceOwnerId, ClientId $clientId, DataBag $parameters, DataBag $metadatas, ? ResourceServerId $resourceServerId): AccessToken
52
    {
53
        $expiresAt = new \DateTimeImmutable(sprintf('now +1800 seconds'));
54
        $length = random_int(50, 100);
55
        $accessTokenId = AccessTokenId::create(Base64Url::encode(random_bytes($length)));
56
        $accessToken = AccessToken::createEmpty();
57
        $accessToken = $accessToken->create($accessTokenId, $resourceOwnerId, $clientId, $parameters, $metadatas, $expiresAt, $resourceServerId);
58
59
        return $accessToken;
60
    }
61
}
62