Issues (26)

src/PdoUserUuidFactory.php (9 issues)

1
<?php
2
namespace Germania\Users;
3
4
use Psr\Container\ContainerInterface;
5
use Ramsey\Uuid\UuidFactoryInterface;
6
7
class PdoUserUuidFactory implements ContainerInterface
8
{
9
10
    /**
11
     * @var string
12
     */
13
    public $table = 'users';
14
15
    /**
16
     * @var string
17
     */
18
    public $users_class;
19
20
21
    /**
22
     * @var PDOStatement
0 ignored issues
show
The type Germania\Users\PDOStatement was not found. Did you mean PDOStatement? If so, make sure to prefix the type with \.
Loading history...
23
     */
24
    public $stmt;
25
26
27
    /**
28
     * @var PDO
29
     */
30
    public $pdo;
31
32
    /**
33
     * @var Ramsey\Uuid\UuidFactory
0 ignored issues
show
The type Germania\Users\Ramsey\Uuid\UuidFactory was not found. Did you mean Ramsey\Uuid\UuidFactory? If so, make sure to prefix the type with \.
Loading history...
34
     */
35
    public $uuid_factory;
36
37
38
    /**
39
     * @param \PDO                 $pdo           PDO instance
40
     * @param UuidFactoryInterface $uuid_factory  Ramsey's UUID factory
41
     * @param UserAbstract|null    $user          Optional: UserInterface instance
42
     * @param [type]               $table         Optional: table name
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
43
     */
44 25
    public function __construct( \PDO $pdo, UuidFactoryInterface $uuid_factory, UserAbstract $user = null, $table = null )
45
    {
46 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...
47 25
        $this->uuid_factory    = $uuid_factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $uuid_factory of type Ramsey\Uuid\UuidFactoryInterface is incompatible with the declared type Germania\Users\Ramsey\Uuid\UuidFactory of property $uuid_factory.

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...
48 25
        $this->table           = $table ?: $this->table;
49 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...
50
51
52
53
        $sql = "SELECT DISTINCT
54
        U.id                 AS id,
55
        LOWER(HEX(U.uuid))   AS uuid,
56
        U.user_first_name    AS first_name,
57
        U.user_first_name    AS first_name,
58
        U.user_last_name     AS last_name,
59
        U.user_login_name    AS login_name,
60
        U.user_display_name  AS display_name,
61
        U.user_email         AS email,
62
        U.api_key            AS api_key
63
64 25
        FROM {$this->table} U
65
66
        WHERE U.uuid = UNHEX( REPLACE( :uuid, '-','') )
67 5
        AND   U.is_active > 0";
68
69 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...
70
71 25
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_users_class );
0 ignored issues
show
$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

71
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, /** @scrutinizer ignore-type */ $this->php_users_class );
Loading history...
72
73 25
    }
74
75
76
    public function __debugInfo() {
77
        return [
78
            'DatabaseTable' => $this->table,
79
            'UsersPhpClass' => $this->php_users_class
80
        ];
81
    }
82
83
84
85 10
    public function has ($uuid) {
86 10
        $uuid = $this->assertUUID( $uuid );
87 10
        if (!$this->stmt->execute([
88 8
            'uuid' => $uuid
89 2
        ])):
90 5
            throw new \RuntimeException("Could not read User from database");
91
        endif;
92
93 5
        return (bool) $this->stmt->fetch();
94
    }
95
96
97 15
    public function get ($uuid) {
98 15
        $uuid = $this->assertUUID( $uuid );
99 15
        if (!$this->stmt->execute([
100 12
            'uuid' => $uuid
101 3
        ])):
102 5
            throw new \RuntimeException("Could not read User from database");
103
        endif;
104
105
106 10
        if ($user = $this->stmt->fetch()) {
107 5
            return $user;
108
        }
109
110 5
        throw new UserNotFoundException("Could not find User with UUID '$uuid'");
111
    }
112
113
114
115
116
    /**
117
     * @param  UuidInterface|string $uuid
118
     * @return UuidInterface
119
     *
120
     * @throws Ramsey\Uuid\Exception\InvalidUuidStringException
121
     */
122 20
    protected function assertUUID( $uuid )
123
    {
124 20
        if (is_string( $uuid )):
125 20
            $uuid = $this->uuid_factory->fromString( $uuid );
126 4
        elseif (!$uuid instanceOf UuidInterface):
0 ignored issues
show
The type Germania\Users\UuidInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
127
            throw new \InvalidArgumentException("UuidInterface or UUID string expected");
128
        endif;
129
130 20
        return $uuid;
131
132
    }
133
}
134
135