PdoDelete::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 1
rs 9.8333
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 deletes any stored persistent login for a given User ID.
9
 */
10
class PdoDelete
11
{
12
13
    /**
14
     * @var string
15
     */
16
    public $table     = "auth_logins";
17
18
    /**
19
     * @var PDOStatement
20
     */
21
    public $stmt;
22
23
24
    /**
25
     * @var LoggerInterface
26
     */
27
    public $logger;
28
29
30
31
    /**
32
     * @param \PDO             $pdo    PDO instance
33
     * @param LoggerInterface  $logger Optional: PSR-3 Logger
34
     * @param string           $table  Optional: Custom table name, default: `auth_logins`
35
     */
36 10 View Code Duplication
    public function __construct( \PDO $pdo, 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...
37
    {
38
39 10
        $this->table  = $table  ?: $this->table;
40 10
        $this->logger = $logger ?: new NullLogger;
41
42
43
        // ------------------------------------------
44
        // 1. Prepare statement
45
        // ------------------------------------------
46 10
        $sql = "DELETE FROM {$this->table}
47 2
        WHERE user_id = :user_id";
48
49 10
        $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...
50 10
    }
51
52
53
    /**
54
     * Deletes all logins for a given User ID.
55
     *
56
     * @param  int $user_id User ID
57
     * @return int Number of deleted rows
58
     */
59 10
    public function __invoke($user_id)
60
    {
61
62 10
        $this->stmt->execute([':user_id' => $user_id]);
63 10
        $deleted = $this->stmt->rowCount();
64
65 10
        $this->logger->info('Deleted persistent logins', [
66 10
            'user_id'      => $user_id,
67 8
            'deleted_rows' => $deleted
68 2
        ]);
69
70 10
        return $deleted;
71
    }
72
73
74
}
75