PdoAssignRoleToPermission::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 10

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 28
loc 28
ccs 0
cts 14
cp 0
rs 9.472
c 0
b 0
f 0
cc 4
nc 10
nop 2
crap 20
1
<?php
2
namespace Germania\Permissions;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\NullLogger;
7
8 View Code Duplication
class PdoAssignRoleToPermission
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 =  "INSERT INTO {$this->permissions_roles_table}
49
        (permission_id, role_id )
50
        VALUES
51
        (:permission_id, :role_id)";
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("Assigned new role to permission", $info);
73
                return (int) $this->pdo->lastInsertId();
74
            endif;
75
76
            $this->logger->error("Could not execute 'Assign role to permission' statement", $info);
77
            return false;
78
        }
79
        catch( \PDOException $e )
80
        {
81
            // SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ...
82
            if ($e->getCode() == 23000):
83
                throw new RolePermissionAssignmentExistsException("Role/Permission assignment already exists", 23000, $e);
84
            endif;
85
86
            throw $e;
87
        }
88
89
    }
90
}
91