PdoAssignUserToRole   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 70
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A __invoke() 0 23 2
1
<?php
2
namespace Germania\UserRoles;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
/**
8
 * This Callable assigns a User ID to a Role ID.
9
 *
10
 * Example:
11
 *
12
 *     <?php
13
 *     use Germania\UserRoles\PdoAssignUserToRole;
14
 *
15
 *     $pdo    = new PDO( ... );
16
 *     $logger = new Monolog();
17
 *     $table  = 'users_roles_mm';
18
 *
19
 *     $assigner = new PdoAssignUserToRole( $pdo, $logger, $table);
20
 *     $assigner( 42, 1);
21
 *     ?>
22
 *
23
 * @author  Carsten Witt <[email protected]>
24
 */
25
class PdoAssignUserToRole
26
{
27
28
    /**
29
     * Database table
30
     * @var string
31
     */
32
    public $table = "users_roles_mm";
33
34
    /**
35
     * @var PDOStatement
36
     */
37
    public $stmt;
38
39
    /**
40
     * @var LoggerInterface
41
     */
42
    public $logger;
43
44
45
    /**
46
     * @param PDO             $pdo         PDO instance
47
     * @param LoggerInterface $logger      Optional: PSR-3 Logger
48
     * @param string          $table       Optional: Database table name
49
     */
50 10
    public function __construct(\PDO $pdo, LoggerInterface $logger = null, $table = null)
51
    {
52
        // Setup
53 10
        $this->table  = $table ?: $this->table;
54 10
        $this->logger = $logger ?: new NullLogger;
55
56
        // Prepare business
57 10
        $sql = "INSERT INTO {$this->table}
58
        (client_id, role_id)
59 2
        VALUES (:client_id, :role_id)";
60
61 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\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...
62 10
    }
63
64
65
    /**
66
     * @param  int  $user_id
67
     * @param  int  $role_id
68
     *
69
     * @return bool
70
     */
71 10
    public function __invoke( $user_id, $role_id )
72
    {
73
74 10
        $result = $this->stmt->execute([
75 10
            'client_id' => $user_id,
76 8
            'role_id'   => $role_id
77 2
        ]);
78
79
        // Evaluate
80
        $loginfo = [
81 10
            'user_id'   => $user_id,
82 10
            'role_id'   => $role_id,
83 8
            'result'    => $result
84 2
        ];
85
86 10
        if ($result):
87 5
            $this->logger->info("Successfully assigned user to role.", $loginfo);
88 1
        else:
89 5
            $this->logger->warning("Could not assign user to role?!", $loginfo);
90
        endif;
91
92 10
        return $result;
93
    }
94
}
95