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

AccessToken::persistToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
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 AbterPhp\Admin\Oauth2\Repository;
6
7
use AbterPhp\Admin\Oauth2\Entity\AccessToken as Entity;
8
use AbterPhp\Admin\Oauth2\Entity\Scope;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Admin\Oauth2\Repository\Scope. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Server\Ent...essTokenEntityInterface 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\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...
11
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Server\Rep...okenRepositoryInterface 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...
12
use Opulence\Databases\ConnectionPools\ConnectionPool;
13
use Opulence\Orm\Ids\Generators\UuidV4Generator;
14
use Opulence\QueryBuilders\MySql\QueryBuilder;
15
16
class AccessToken implements AccessTokenRepositoryInterface
17
{
18
    /** @var UuidV4Generator */
19
    protected $uuidGenerator;
20
21
    /** @var ConnectionPool */
22
    protected $connectionPool;
23
24
    /**
25
     * AccessToken constructor.
26
     *
27
     * @param UuidV4Generator $uuidGenerator
28
     * @param ConnectionPool  $connectionPool
29
     */
30
    public function __construct(UuidV4Generator $uuidGenerator, ConnectionPool $connectionPool)
31
    {
32
        $this->uuidGenerator  = $uuidGenerator;
33
        $this->connectionPool = $connectionPool;
34
    }
35
36
    /**
37
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
38
     *
39
     * @param ClientEntityInterface $clientEntity
40
     * @param array                 $scopes
41
     * @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...
42
     *
43
     * @return AccessTokenEntityInterface
44
     */
45
    public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
46
    {
47
        $accessToken = new Entity();
48
49
        return $accessToken;
50
    }
51
52
    /**
53
     * @param AccessTokenEntityInterface $accessTokenEntity
54
     */
55
    public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
56
    {
57
        $this->persistToken($accessTokenEntity);
58
59
        foreach ($accessTokenEntity->getScopes() as $scope) {
60
            $this->persistTokenScope($accessTokenEntity, $scope);
61
        }
62
    }
63
64
    /**
65
     * @param AccessTokenEntityInterface $accessTokenEntity
66
     */
67
    protected function persistToken(AccessTokenEntityInterface $accessTokenEntity)
68
    {
69
        $data = [
70
            'id'            => $accessTokenEntity->getIdentifier(),
71
            'api_client_id' => $accessTokenEntity->getClient()->getName(),
72
            'expires_at'    => $accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s'),
73
        ];
74
75
        $query = (new QueryBuilder())->insert('tokens', $data);
76
77
        $sql    = $query->getSql();
78
        $params = array_values($data);
79
80
        $connection = $this->connectionPool->getWriteConnection();
81
        $statement  = $connection->prepare($sql);
82
        $statement->bindValues($params);
83
        $statement->execute();
84
    }
85
86
    /**
87
     * @param AccessTokenEntityInterface $accessTokenEntity
88
     * @param Scope                      $scope
89
     */
90
    protected function persistTokenScope(AccessTokenEntityInterface $accessTokenEntity, Scope $scope)
91
    {
92
        $data = [
93
            'id'                => $this->uuidGenerator->generate(null),
94
            'token_id'          => $accessTokenEntity->getIdentifier(),
95
            'admin_resource_id' => $scope->getIdentifier(),
96
        ];
97
98
        $query = (new QueryBuilder())->insert('tokens_admin_resources', $data);
99
100
        $sql    = $query->getSql();
101
        $params = array_values($data);
102
103
        $connection = $this->connectionPool->getWriteConnection();
104
        $statement  = $connection->prepare($sql);
105
        $statement->bindValues($params);
106
        $statement->execute();
107
    }
108
109
    /**
110
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
111
     *
112
     * @param string $tokenId
113
     */
114
    public function revokeAccessToken($tokenId)
115
    {
116
    }
117
118
    /**
119
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
120
     *
121
     * @param string $tokenId
122
     *
123
     * @return bool
124
     */
125
    public function isAccessTokenRevoked($tokenId)
126
    {
127
        return false;
128
    }
129
}
130