Test Setup Failed
Push — dev ( b27119...389162 )
by Herberto
04:46
created

OauthClientRepository::findActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Acme\App\Infrastructure\Auth\Authentication\Oauth;
6
7
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
8
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
9
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
10
use League\OAuth2\Server\Entities\ClientEntityInterface;
11
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
12
13
final class OauthClientRepository implements ClientRepositoryInterface
14
{
15
    /**
16
     * @var DqlQueryBuilderInterface
17
     */
18
    private $dqlQueryBuilder;
19
20
    /**
21
     * @var QueryServiceRouterInterface
22
     */
23
    private $queryService;
24
25
    public function __construct(
26
        DqlQueryBuilderInterface $dqlQueryBuilder,
27
        QueryServiceRouterInterface $queryService
28
    ) {
29
        $this->dqlQueryBuilder = $dqlQueryBuilder;
30
        $this->queryService = $queryService;
31
    }
32
33
    /**
34
     * @param string $clientIdentifier The client's identifier
35
     * @param null|string $grantType The grant type used (if sent)
36
     * @param null|string $clientSecret The client's secret (if sent)
37
     * @param bool $mustValidateSecret If true the client must attempt to validate the secret if the client
38
     *                                        is confidential
39
     */
40
    public function getClientEntity(
41
        $clientIdentifier,
42
        $grantType = null,
43
        $clientSecret = null,
44
        $mustValidateSecret = true
45
    ): ?ClientEntityInterface {
46
        try {
47
            $oauthClient = $this->findActive($clientIdentifier);
48
        } catch (EmptyQueryResultException $e) {
49
            return null;
50
        }
51
52
        if ($mustValidateSecret && !hash_equals($oauthClient->getSecret(), (string) $clientSecret)) {
53
            return null;
54
        }
55
56
        return $oauthClient;
57
    }
58
59
    private function findActive(string $clientIdentifier): OauthClient
60
    {
61
        $dqlQuery = $this->dqlQueryBuilder->create(OauthClient::class)
62
            ->where('OauthClient.identifier = :clientIdentifier')
63
            ->andWhere('OauthClient.active = :active')
64
            ->setParameter('clientIdentifier', $clientIdentifier)
65
            ->setParameter('active', true)
66
            ->build();
67
68
        return $this->queryService->query($dqlQuery)->getSingleResult();
69
    }
70
}
71