Completed
Push — develop ( 529afd...b3999a )
by Carsten
06:32
created

PdoUserFactory::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 10
    /**
33
     * @param PDO           $pdo
34 10
     * @param UserAbstract  $user   Optional: User template object
35 10
     * @param string        $table  Optional: Users table name
36
     */
37
    public function __construct( \PDO $pdo, UserAbstract $user = null, $table = null )
38
    {
39
        $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
        $this->table           = $table ?: $this->table;
41
        $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 10
        U.id                 AS id,
48
        U.user_first_name    AS first_name,
49
        U.user_last_name     AS last_name,
50 5
        U.user_login_name    AS login_name,
51
        U.user_display_name  AS display_name,
52 10
        U.user_email         AS email,
53
        U.api_key            AS api_key
54 10
55
        FROM {$this->table} U
56 10
57
        WHERE U.id = :id
58
        AND   U.is_active > 0";
59 4
60 4
        $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...
61 2
62 2
        $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

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