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 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
|
|
|
|
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.