Failed Conditions
Push — master ( 3df084...2d5f15 )
by Florent
06:05
created

AccessTokenRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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 Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use OAuth2Framework\Component\Core\AccessToken\AccessToken as CoreAccessToken;
19
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId;
20
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository as AccessTokenRepositoryInterface;
21
use OAuth2Framework\Component\Core\Client\ClientId;
22
use OAuth2Framework\Component\Core\DataBag\DataBag;
23
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
24
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
25
26
final class AccessTokenRepository implements AccessTokenRepositoryInterface, ServiceEntityRepositoryInterface
27
{
28
    private $entityRepository;
29
    private $entityManager;
30
31
    public function __construct(ManagerRegistry $managerRegistry)
32
    {
33
        $this->entityManager = $managerRegistry->getManagerForClass(AccessToken::class);
34
        $this->entityRepository = $this->entityManager->getRepository(AccessToken::class);
35
    }
36
37
    public function find(AccessTokenId $accessTokenId): ?CoreAccessToken
38
    {
39
        return $this->entityRepository->find($accessTokenId);
40
    }
41
42
    public function save(CoreAccessToken $accessToken): void
43
    {
44
        if (!$accessToken instanceof AccessToken) {
45
            throw new \InvalidArgumentException('Unsupported access token class');
46
        }
47
        $this->entityManager->persist($accessToken);
48
        $this->entityManager->flush();
49
    }
50
51
    public function create(ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId): CoreAccessToken
52
    {
53
        return new AccessToken(new AccessTokenId(\bin2hex(\random_bytes(32))), $clientId, $resourceOwnerId, $expiresAt, $parameter, $metadata, $resourceServerId);
54
    }
55
}
56