Completed
Push — master ( 96a86d...9a9433 )
by Neomerx
02:30
created

TokenRepository::readUserByToken()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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