PdoValidator::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 4
rs 9.568
c 0
b 0
f 0
1
<?php
2
namespace Germania\PermanentAuth;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
/**
8
 * This Callable finds on the server-side the user ID for a given selector and token pair.
9
 */
10
class PdoValidator
11
{
12
    /**
13
     * @var LoggerInterface
14
     */
15
    public $logger;
16
17
18
    /**
19
     * @var PDOStatement
20
     */
21
    public $stmt;
22
23
    /**
24
     * @var Callable
25
     */
26
    public $verifier;
27
28
29
    /**
30
     * @var string
31
     */
32
    public $table = "auth_logins";
33
34
35
    /**
36
     * @param \PDO                 $pdo      PDO instance
37
     * @param Callable             $verifier Token authentication that accepts selector and token
38
     * @param LoggerInterface|null $logger   Optional: PSR-3 Logger
39
     * @param string               $table    Optional: Custom table name, default: `auth_logins`
40
     */
41 20 View Code Duplication
    public function __construct( \PDO $pdo, Callable $verifier, LoggerInterface $logger = null, $table = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 20
        $this->verifier = $verifier;
44 20
        $this->logger   = $logger;
45 20
        $this->table    = $table ?: $this->table;
46
47
        $sql = "SELECT
48
        user_id,
49
        token_hash
50 20
        FROM {$this->table}
51
        WHERE selector = :selector
52
        AND valid_until >= NOW()
53 4
        LIMIT 1";
54
55 20
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type object<PDOStatement> is incompatible with the declared type object<Germania\PermanentAuth\PDOStatement> of property $stmt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56 20
    }
57
58
59
    /**
60
     * Finds the user ID for a matching selector and token pair.
61
     *
62
     * @param  string $selector Login selector
63
     * @param  string $token    Login token
64
     * @return mixed            User ID or FALSE
65
     */
66 15
    public function __invoke( $selector, $token )
67
    {
68 15
        $this->stmt->execute([
69 12
            'selector' => $selector
70 3
        ]);
71
72 15
        if (!$row = $this->stmt->fetch( \PDO::FETCH_OBJ )):
73 5
            $this->logger->info("No matching row found");
74 5
            return false;
75
        else:
76 10
            $this->logger->debug("Found row", [
77 10
                'user_id' => $row->user_id
78 2
            ]);
79
        endif;
80
81 10
        $verifier = $this->verifier;
82
83 10
        $result = $verifier($token, $row->token_hash);
84 10
        $this->logger->info("Token match: " . ($result ? "YES" : "NO"));
85
86 10
        return $result ? $row->user_id : false;
87
    }
88
}
89