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

ClientRepository::populateClients()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 8.8945
c 0
b 0
f 0
cc 1
nc 1
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ClientRepository::create() 0 4 1
A ClientRepository::createClientId() 0 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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