Passed
Push — master ( 40e145...79d0e7 )
by Derek Stephen
02:48
created

ClientRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace OAuth\Repository;
4
5
use Doctrine\ORM\UnitOfWork;
6
use OAuth\Client;
7
use Doctrine\ORM\EntityRepository;
8
use League\OAuth2\Server\Entities\ClientEntityInterface;
9
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
10
11
class ClientRepository extends EntityRepository implements ClientRepositoryInterface
12
{
13
    /**
14
     * @param string $clientIdentifier
15
     * @param null|string $grantType
16
     * @param null|string|null $clientSecret
17
     * @param bool $mustValidateSecret
18
     *
19
     * @return ClientEntityInterface|null
20
     */
21 3
    public function getClientEntity($clientIdentifier, $grantType = null, $clientSecret = null, $mustValidateSecret = true)
22
    {
23
        /** @var Client $client */
24 3
        $client = $this->findOneBy([
25 3
            'identifier' => $clientIdentifier
26
        ]);
27
28 3
        if ($client instanceof Client == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
29 1
            return null;
30
        }
31
32
        if (
33 2
            $mustValidateSecret === true
34 2
            && $client->isConfidential() === true
35 2
            && $clientSecret != $client->getSecret()
36
        ) {
37 1
            return null;
38
        }
39
40 1
        return $client;
41
    }
42
43
    /**
44
     * @param Client $client
45
     * @return Client
46
     * @throws \Doctrine\ORM\OptimisticLockException
47
     */
48
    public function create(Client $client)
49
    {
50
        $em = $this->getEntityManager();
51
        $user = $client->getUser();
52
        if ($em->getUnitOfWork()->getEntityState($user) !== UnitOfWork::STATE_MANAGED) {
53
            $user = $em->merge($user);
54
            $client->setUser($user);
55
        }
56
        $em->persist($client);
57
        $em->flush();
58
        return $client;
59
    }
60
61
    /**
62
     * @param Client $client
63
     * @return Client
64
     * @throws \Doctrine\ORM\OptimisticLockException
65
     */
66
    public function save(Client $client)
67
    {
68
        $this->_em->flush($client);
69
        return $client;
70
    }
71
72
    /**
73
     * @param Client $client
74
     * @throws \Doctrine\ORM\OptimisticLockException
75
     */
76
    public function delete(Client $client)
77
    {
78
        $this->_em->remove($client);
79
        $this->_em->flush($client);
80
    }
81
}