Scope::queryWithoutUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 9.8333
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Oauth2\Repository;
6
7
use AbterPhp\Admin\Exception\Database;
8
use AbterPhp\Admin\Oauth2\Entity\Scope as Entity;
9
use League\OAuth2\Server\Entities\ClientEntityInterface;
10
use League\OAuth2\Server\Entities\ScopeEntityInterface;
11
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
12
use Opulence\Databases\ConnectionPools\ConnectionPool;
13
use Opulence\QueryBuilders\Conditions\ConditionFactory;
14
use Opulence\QueryBuilders\MySql\QueryBuilder;
15
16
/** @phan-file-suppress PhanTypeMismatchArgument */
17
class Scope implements ScopeRepositoryInterface
18
{
19
    /** @var ConnectionPool */
20
    protected $connectionPool;
21
22
    /**
23
     * Scope constructor.
24
     *
25
     * @param ConnectionPool $connectionPool
26
     */
27
    public function __construct(ConnectionPool $connectionPool)
28
    {
29
        $this->connectionPool = $connectionPool;
30
    }
31
32
    /**
33
     * @param string $identifier
34
     *
35
     * @return ScopeEntityInterface
36
     */
37
    public function getScopeEntityByIdentifier($identifier)
38
    {
39
        $scopeData = $this->queryWithoutUser($identifier);
40
41
        $scope = new Entity($scopeData['id']);
42
43
        return $scope;
44
    }
45
46
    /**
47
     * @param string $clientId
48
     *
49
     * @return array|bool
50
     */
51
    protected function queryWithoutUser(string $clientId)
52
    {
53
        $query = (new QueryBuilder())
54
            ->select('ar.id')
55
            ->from('admin_resources', 'ar')
56
            ->where('ar.deleted_at IS NULL')
57
            ->andWhere('ar.identifier = :identifier');
58
59
        $sql    = $query->getSql();
60
        $params = ['identifier' => [$clientId, \PDO::PARAM_STR]];
61
62
        $connection = $this->connectionPool->getReadConnection();
63
        $statement  = $connection->prepare($sql);
64
        $statement->bindValues($params);
65
        if (!$statement->execute()) {
66
            throw new Database($statement->errorInfo());
67
        }
68
69
        return $statement->fetch(\PDO::FETCH_ASSOC);
70
    }
71
72
    /**
73
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
74
     *
75
     * @param Entity[]              $scopes
76
     * @param string                $grantType
77
     * @param ClientEntityInterface $clientEntity
78
     * @param null                  $userIdentifier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userIdentifier is correct as it would always require null to be passed?
Loading history...
79
     *
80
     * @return ScopeEntityInterface[]
81
     */
82
    public function finalizeScopes(
83
        array $scopes,
84
        $grantType,
85
        ClientEntityInterface $clientEntity,
86
        $userIdentifier = null
87
    ) {
88
        $scopeIds = [];
89
        foreach ($scopes as $scope) {
90
            $scopeIds[] = $scope->getIdentifier();
91
        }
92
93
        if (empty($scopeIds)) {
94
            return [];
95
        }
96
97
        $finalScopes = [];
98
        foreach ($this->checkScopes($clientEntity->getIdentifier(), $scopeIds) as $scopeData) {
99
            $finalScopes[] = new Entity($scopeData['admin_resource_id']);
100
        }
101
102
        return $finalScopes;
103
    }
104
105
    /**
106
     * @param string   $clientId
107
     * @param string[] $scopeIds
108
     *
109
     * @return string[][]
110
     */
111
    protected function checkScopes(string $clientId, array $scopeIds): array
112
    {
113
        $scopeIdIn = [];
114
        foreach ($scopeIds as $scopeId) {
115
            $scopeIdIn[] = [$scopeId, \PDO::PARAM_STR];
116
        }
117
118
        $conditions = new ConditionFactory();
119
        $query      = (new QueryBuilder())
120
            ->select('acar.admin_resource_id')
121
            ->from('api_clients_admin_resources', 'acar')
122
            ->andWhere('acar.api_client_id = ?')
123
            ->andWhere($conditions->in('acar.admin_resource_id', $scopeIdIn));
124
125
        $sql    = $query->getSql();
126
        $params = array_merge([[$clientId, \PDO::PARAM_STR]], $scopeIdIn);
127
128
        $connection = $this->connectionPool->getReadConnection();
129
        $statement  = $connection->prepare($sql);
130
        $statement->bindValues($params);
131
        if (!$statement->execute()) {
132
            throw new Database($statement->errorInfo());
133
        }
134
135
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
136
    }
137
}
138