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

Scope::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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