PdoAllPermissions::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace Germania\Permissions;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\NullLogger;
7
use Psr\Container\ContainerInterface;
8
use Psr\Container\NotFoundException;
9
10
class PdoAllPermissions implements \Countable, \IteratorAggregate, ContainerInterface
11
{
12
13
    use LoggerAwareTrait;
14
15
    /**
16
     * @var \PDOStatement
17
     */
18
    public $stmt;
19
20
    /**
21
     * @var Array
22
     */
23
    public $permissions = array();
24
25
    /**
26
     * @var string
27
     */
28
    public $permissions_table = "permissions";
29
30
    /**
31
     * @var string
32
     */
33
    public $permissions_roles_table = "permissions_roles";
34
35
    /**
36
     * @var Seperator string for roles in SELECT statement
37
     */
38
    protected $separator = ",";
39
40
41
    /**
42
     * @param \PDO                 $pdo                      PDO instance
43
     * @param string               $permissions_table        Permissions table name
44
     * @param string               $permissions_roles_table  Permissions and roles assignments table
45
     * @param LoggerInterface|null $logger                   Optional: PSR-3 Logger
46
     */
47
    public function __construct( \PDO $pdo, $permissions_table, $permissions_roles_table, LoggerInterface $logger = null )
48
    {
49
        $this->setLogger( $logger ?: new NullLogger );
50
51
        // Prerequisites
52
        $this->permissions_table       = $permissions_table;
53
        $this->permissions_roles_table = $permissions_roles_table;
54
55
        // Read pages and allowed roles
56
        $sql =  "SELECT
57
        -- Select name twice here because of UNIQUE
58
        P.permission_name AS name,
59
        P.id,
60
        P.permission_name AS name,
61
        P.permission_description AS description,
62
        P.info AS info,
63
        GROUP_CONCAT(Pmm.role_id ORDER BY role_id ASC SEPARATOR '{$this->separator}') AS assigned_roles
64
65
        FROM {$this->permissions_table} P
66
        LEFT JOIN {$this->permissions_roles_table} Pmm
67
        ON P.id = Pmm.permission_id
68
69
        GROUP BY name";
70
71
        // Prepare business
72
        $this->stmt = $pdo->prepare( $sql );
73
74
        $this->stmt->execute();
75
76
        $this->permissions = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE | \PDO::FETCH_ASSOC);
77
    }
78
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function has( $name )
84
    {
85
        return array_key_exists($name, $this->permissions);
86
    }
87
88
89
    /**
90
     * @inheritDoc
91
     * @throws PermissionNotFoundException
92
     */
93
    public function get( $name )
94
    {
95
        if ($this->has($name))
96
            return $this->permissions[ $name ];
97
        throw new PermissionNotFoundException("There is no permission '$name'");
98
    }
99
100
101
    /**
102
     * @inheritDoc
103
     * @return int
104
     */
105
    public function count()
106
    {
107
        return count( $this->permissions );
108
    }
109
110
    /**
111
     * @return ArrayIterator
112
     */
113
    public function getIterator()
114
    {
115
        return new \ArrayIterator( $this->permissions );
116
    }
117
}
118