PdoUserFactory::has()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
namespace Germania\Users;
3
4
use Psr\Container\ContainerInterface;
5
6
class PdoUserFactory implements ContainerInterface
7
{
8
9
    /**
10
     * @var string
11
     */
12
    public $table = 'users';
13
14
    /**
15
     * @var string
16
     */
17
    public $users_class;
18
19
20
    /**
21
     * @var PDOStatement
0 ignored issues
show
Bug introduced by
The type Germania\Users\PDOStatement was not found. Did you mean PDOStatement? If so, make sure to prefix the type with \.
Loading history...
22
     */
23
    public $stmt;
24
25
26
    /**
27
     * @var PDO
28
     */
29
    public $pdo;
30
31
32
    /**
33
     * @param PDO           $pdo
34
     * @param UserAbstract  $user   Optional: User template object
35
     * @param string        $table  Optional: Users table name
36
     */
37 25
    public function __construct( \PDO $pdo, UserAbstract $user = null, $table = null )
38
    {
39 25
        $this->pdo             = $pdo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo of type PDO is incompatible with the declared type Germania\Users\PDO of property $pdo.

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...
40 25
        $this->table           = $table ?: $this->table;
41 25
        $this->php_users_class = $user ? get_class($user) : User::class;
0 ignored issues
show
Bug Best Practice introduced by
The property php_users_class does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
43
44
45
        // ID is listed twice here in order to use it with FETCH_UNIQUE as array key
46
        $sql = "SELECT DISTINCT
47
        U.id                 AS id,
48
        LOWER(HEX(U.uuid))   AS uuid,
49
        U.user_first_name    AS first_name,
50
        U.user_last_name     AS last_name,
51
        U.user_login_name    AS login_name,
52
        U.user_display_name  AS display_name,
53
        U.user_email         AS email,
54
        U.api_key            AS api_key
55
56 25
        FROM {$this->table} U
57
58
        WHERE U.id = :id
59 5
        AND   U.is_active > 0";
60
61 25
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type PDOStatement or boolean is incompatible with the declared type Germania\Users\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...
62
63 25
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_users_class );
0 ignored issues
show
Bug introduced by
$this->php_users_class of type string is incompatible with the type integer expected by parameter $columnNumber of PDOStatement::setFetchMode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, /** @scrutinizer ignore-type */ $this->php_users_class );
Loading history...
64
65 25
    }
66
67
68
    public function __debugInfo() {
69
        return [
70
            'DatabaseTable' => $this->table,
71
            'UsersPhpClass' => $this->php_users_class
72
        ];
73
    }
74
75
76
77 10
    public function has ($id) {
78 10
        if (!$this->stmt->execute([
79 8
            'id' => $id
80 2
        ])):
81 5
            throw new \RuntimeException("Could not read User from database");
82
        endif;
83
84 5
        return (bool) $this->stmt->fetch();
85
    }
86
87
88 15
    public function get ($id) {
89 15
        if (!$this->stmt->execute([
90 12
            'id' => $id
91 3
        ])):
92 5
            throw new \RuntimeException("Could not read User from database");
93
        endif;
94
95
96 10
        if ($user = $this->stmt->fetch()) {
97 5
            return $user;
98
        }
99
100 5
        throw new UserNotFoundException("Could not find User with ID '$id'");
101
    }
102
103
}
104
105