PdoUnassignRoleFromPermission   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 78
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A __invoke() 0 23 3
1
<?php
2
namespace Germania\Permissions;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\NullLogger;
7
8
class PdoUnassignRoleFromPermission
9
{
10
11
    use LoggerAwareTrait;
12
13
    /**
14
     * @var \PDO
15
     */
16
    public $pdo;
17
18
    /**
19
     * @var \PDOStatement
20
     */
21
    public $stmt;
22
23
    /**
24
     * @var string
25
     */
26
    public $permissions_roles_table = "permissions_roles";
27
28
    /**
29
     * @var Seperator string for roles in SELECT statement
30
     */
31
    protected $separator = ",";
32
33
34
    /**
35
     * @param \PDO                 $pdo                      PDO instance
36
     * @param string               $permissions_roles_table  Permissions/Roles table name
37
     * @param LoggerInterface|null $logger                   Optional: PSR-3 Logger
38
     */
39
    public function __construct( \PDO $pdo, $permissions_roles_table, LoggerInterface $logger = null )
40
    {
41
        $this->setLogger( $logger ?: new NullLogger );
42
43
        // Prerequisites
44
        $this->pdo                     = $pdo;
45
        $this->permissions_roles_table = $permissions_roles_table;
46
47
        // Read pages and allowed roles
48
        $sql =  "DELETE FROM {$this->permissions_roles_table}
49
        WHERE permission_id = :permission_id
50
        AND role_id = :role_id
51
        LIMIT 1";
52
53
        // Prepare business
54
        $this->stmt = $pdo->prepare( $sql );
55
    }
56
57
58
    /**
59
     * @return int Last Insert ID or FALSE when something errored
60
     * @throws PermissionNameExistsException On duplicate name
61
     */
62
    public function __invoke( $permission_id, $role_id )
63
    {
64
        try {
65
            $info = [
66
                'permission_id' => $permission_id,
67
                'role_id' => $role_id
68
            ];
69
70
            $result = $this->stmt->execute( $info );
71
            if ($result):
72
                $this->logger->info("Unassigned role from permission", $info);
73
                return true;
74
            endif;
75
76
            $this->logger->error("Could not execute 'Unassign role from permission' statement", $info);
77
            return false;
78
        }
79
        catch( \PDOException $e )
80
        {
81
            throw $e;
82
        }
83
84
    }
85
}
86