Failed Conditions
Push — master ( 9eeb29...881d26 )
by Florent
16:44
created

ClientRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 3
dl 0
loc 35
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 4 1
A save() 0 8 2
A create() 0 4 1
A createClientId() 0 4 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\Client\Client as ClientInterface;
19
use OAuth2Framework\Component\Core\Client\ClientId;
20
use OAuth2Framework\Component\Core\Client\ClientRepository as ClientRepositoryInterface;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
23
24
final class ClientRepository implements ClientRepositoryInterface, ServiceEntityRepositoryInterface
25
{
26
    private $entityRepository;
27
    private $entityManager;
28
29
    public function __construct(ManagerRegistry $managerRegistry)
30
    {
31
        $this->entityManager = $managerRegistry->getManagerForClass(Client::class);
32
        $this->entityRepository = $this->entityManager->getRepository(Client::class);
33
    }
34
35
    public function find(ClientId $clientId): ?ClientInterface
36
    {
37
        return $this->entityRepository->find($clientId);
38
    }
39
40
    public function save(ClientInterface $client)
41
    {
42
        if (!$client instanceof Client) {
43
            throw new \InvalidArgumentException('Unsupported client class');
44
        }
45
        $this->entityManager->persist($client);
46
        $this->entityManager->flush();
47
    }
48
49
    public function create(ClientId $clientId, DataBag $parameters, ?UserAccountId $ownerId): ClientInterface
50
    {
51
        return new Client($clientId, $parameters, $ownerId);
52
    }
53
54
    public function createClientId(): ClientId
55
    {
56
        return new ClientId(\bin2hex(random_bytes(32)));
57
    }
58
}
59