PdoUserRoles   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 51
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A __invoke() 0 11 2
1
<?php
2
namespace Germania\UserRoles;
3
4
class PdoUserRoles
5
{
6
7
    /**
8
     * @var string
9
     */
10
    public $table = 'users_roles_mm';
11
12
13
    /**
14
     * @var PDOStatement
15
     */
16
    public $stmt;
17
18
19
    /**
20
     * @param PDO           $pdo
21
     * @param string        $table  Optional: Users table name
22
     */
23 20
    public function __construct( \PDO $pdo, $table = null  )
24
    {
25 20
        $this->table = $table ?: $this->table;
26
27
        // ID is listed twice here in order to use it with FETCH_UNIQUE as array key
28
        $sql = "SELECT
29
        role_id
30 20
        FROM {$this->table}
31 4
        WHERE client_id = :user_id";
32
33
        // Store for later use
34 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\UserRoles\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...
35 20
    }
36
37
38
    /**
39
     * @param  int   $user_id User ID
40
     * @return int[] Array of Role IDs
41
     */
42 20
    public function __invoke( $user_id ) {
43 20
        $result = $this->stmt->execute([
44 16
            'user_id' => $user_id
45 4
        ]);
46
47 20
        if ($result) {
48 10
            return $this->stmt->fetchAll(\PDO::FETCH_COLUMN);
49
        }
50
51 10
        throw new \RuntimeException( "PDOStatement execution on table " . $this->table . " returned FALSE");
52
    }
53
54
}
55
56