Code Duplication    Length = 80-83 lines in 2 locations

src/PdoAddPermission.php 1 location

@@ 8-87 (lines=80) @@
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\NullLogger;
7
8
class PdoAddPermission
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_table = "permissions";
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_table        Permissions table name
37
     * @param LoggerInterface|null $logger                   Optional: PSR-3 Logger
38
     */
39
    public function __construct( \PDO $pdo, $permissions_table, LoggerInterface $logger = null )
40
    {
41
        $this->setLogger( $logger ?: new NullLogger );
42
43
        // Prerequisites
44
        $this->pdo                     = $pdo;
45
        $this->permissions_table       = $permissions_table;
46
47
        // Read pages and allowed roles
48
        $sql =  "INSERT INTO {$this->permissions_table}
49
        (permission_name, permission_description, info )
50
        VALUES
51
        (:name, :description, :info)";
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( array $permission )
63
    {
64
        $permission = array_merge([ 'info' => null, ], $permission);
65
66
        try {
67
            $result = $this->stmt->execute( $permission );
68
            if ($result):
69
                $this->logger->info("Added new permission", $permission);
70
                return (int) $this->pdo->lastInsertId();
71
            endif;
72
73
            $this->logger->error("Could not execute 'Add permission' statement", $permission);
74
            return false;
75
        }
76
        catch( \PDOException $e )
77
        {
78
            // SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ...
79
            if ($e->getCode() == 23000):
80
                throw new PermissionNameExistsException("Permission name already exists", 23000, $e);
81
            endif;
82
83
            throw $e;
84
        }
85
86
    }
87
}
88

src/PdoAssignRoleToPermission.php 1 location

@@ 8-90 (lines=83) @@
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\NullLogger;
7
8
class PdoAssignRoleToPermission
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