TokenRepository::getTableNameForReading()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Adaptors\PostgreSql;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Doctrine\DBAL\Connection;
22
use Doctrine\DBAL\DBALException;
23
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface;
24
use Limoncello\Passport\Exceptions\RepositoryException;
25
use PDO;
26
27
/**
28
 * @package Limoncello\Passport
29
 */
30
class TokenRepository extends \Limoncello\Passport\Repositories\TokenRepository
31
{
32
    use ArrayParserTrait;
33
34
    /**
35
     * @var string
36
     */
37
    private $modelClass;
38
39
    /**
40
     * @param Connection              $connection
41
     * @param DatabaseSchemaInterface $databaseSchema
42
     * @param string                  $modelClass
43
     */
44 4
    public function __construct(
45
        Connection $connection,
46
        DatabaseSchemaInterface $databaseSchema,
47
        string $modelClass = Token::class
48
    ) {
49 4
        $this->setConnection($connection)->setDatabaseSchema($databaseSchema);
50 4
        $this->modelClass = $modelClass;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     *
56
     * @throws RepositoryException
57
     */
58 2
    public function readPassport(string $tokenValue, int $expirationInSeconds): ?array
59
    {
60
        try {
61 2
            $schema = $this->getDatabaseSchema();
62 2
            $query  = $this->getConnection()->createQueryBuilder();
63 2
            $query  = $this->addExpirationCondition(
64 2
                $query->select(['*'])
65 2
                    ->from($schema->getPassportView())
66 2
                    ->where($schema->getTokensValueColumn() . '=' . $this->createTypedParameter($query, $tokenValue))
67 2
                    ->andWhere($query->expr()->eq($this->getDatabaseSchema()->getTokensIsEnabledColumn(), "'1'")),
68 2
                $expirationInSeconds,
69 2
                $schema->getTokensValueCreatedAtColumn()
70
            );
71
72 2
            $statement = $query->execute();
73 1
            $statement->setFetchMode(PDO::FETCH_ASSOC);
74 1
            $data = $statement->fetch();
75
76 1
            $result = null;
77 1
            if ($data !== false) {
78 1
                $scopesColumn        = $schema->getTokensViewScopesColumn();
79 1
                $scopeList           = $data[$scopesColumn];
80 1
                $data[$scopesColumn] = $this->parseArray($scopeList);
81 1
                $result              = $data;
82
            }
83
84 1
            return $result;
85 1
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (DBALException $exception) {
86 1
            $message = 'Passport reading failed.';
87 1
            throw new RepositoryException($message, 0, $exception);
88
        }
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    protected function getClassName(): string
95
    {
96 1
        return $this->modelClass;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    protected function getTableNameForReading(): string
103
    {
104 1
        return $this->getDatabaseSchema()->getTokensView();
105
    }
106
}
107