Passed
Push — master ( 1d30f9...202808 )
by Peter
04:48
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 26
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getClientEntity() 0 20 3
A query() 0 20 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Oauth2\Repository;
6
7
use AbterPhp\Admin\Oauth2\Entity\Client as Entity;
8
use AbterPhp\Framework\Crypto\Crypto;
9
use League\OAuth2\Server\Entities\ClientEntityInterface;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Server\Ent...s\ClientEntityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Server\Rep...ientRepositoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Opulence\Databases\ConnectionPools\ConnectionPool;
12
use Opulence\QueryBuilders\MySql\QueryBuilder;
13
14
class Client implements ClientRepositoryInterface
15
{
16
    /** @var Crypto */
17
    protected $crypto;
18
19
    /** @var ConnectionPool */
20
    protected $connectionPool;
21
22
    /**
23
     * Client constructor.
24
     *
25
     * @param Crypto         $crypto
26
     * @param ConnectionPool $connectionPool
27
     */
28
    public function __construct(Crypto $crypto, ConnectionPool $connectionPool)
29
    {
30
        $this->crypto         = $crypto;
31
        $this->connectionPool = $connectionPool;
32
    }
33
34
    /**
35
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
36
     *
37
     * @param string $clientIdentifier
38
     * @param null   $grantType
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $grantType is correct as it would always require null to be passed?
Loading history...
39
     * @param null   $clientSecret
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $clientSecret is correct as it would always require null to be passed?
Loading history...
40
     * @param bool   $mustValidateSecret
41
     *
42
     * @return ClientEntityInterface
43
     */
44
    public function getClientEntity(
45
        $clientIdentifier,
46
        $grantType = null,
47
        $clientSecret = null,
48
        $mustValidateSecret = true
49
    ) {
50
        $clientData = $this->query($clientIdentifier);
51
52
        if (empty($clientData['secret'])) {
53
            return null;
54
        }
55
56
        $clientSecret = $this->crypto->prepareSecret($clientSecret);
0 ignored issues
show
Bug introduced by
$clientSecret of type null is incompatible with the type string expected by parameter $rawText of AbterPhp\Framework\Crypto\Crypto::prepareSecret(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $clientSecret = $this->crypto->prepareSecret(/** @scrutinizer ignore-type */ $clientSecret);
Loading history...
57
        if (!$this->crypto->verifySecret($clientSecret, $clientData['secret'])) {
58
            return null;
59
        }
60
61
        $client = new Entity($clientIdentifier, $clientIdentifier, '');
62
63
        return $client;
64
    }
65
66
    /**
67
     * @param string $clientId
68
     *
69
     * @return array|bool
70
     */
71
    protected function query(string $clientId)
72
    {
73
        // TODO: Implement getClientEntity() method.
74
        $query = (new QueryBuilder())
75
            ->select('ac.secret')
76
            ->from('api_clients', 'ac')
77
            ->where('ac.deleted = 0')
78
            ->andWhere('ac.id = :clientId');
79
80
        $sql    = $query->getSql();
81
        $params = ['clientId' => $clientId];
82
83
        $connection = $this->connectionPool->getReadConnection();
84
        $statement  = $connection->prepare($sql);
85
        $statement->bindValues($params);
86
        if (!$statement->execute()) {
87
            return false;
88
        }
89
90
        return $statement->fetch(\PDO::FETCH_ASSOC);
91
    }
92
}
93