PdoWebsiteRoutesAcl   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 73
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 4
A __invoke() 0 11 1
1
<?php
2
namespace Germania\Websites;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
use Psr\Log\LoggerAwareTrait;
7
8
class PdoWebsiteRoutesAcl
9
{
10
11
    use LoggerAwareTrait;
12
13
14
    /**
15
     * @var \PDOStatement
16
     */
17
    public $stmt;
18
19
20
    /**
21
     * @var string
22
     */
23
    public $pages_table = "pages";
24
25
    /**
26
     * @var string
27
     */
28
    public $pages_roles_table   = "pages_roles";
29
30
    /**
31
     * @var string
32
     */
33
    public $separator   = ",";
34
35
36
    /**
37
     * @param \PDO                 $pdo                 PDO instance
38
     * @param string               $pages_table         Pages table name
39
     * @param string               $pages_roles_table   Pages and roles assignments table name
40
     * @param LoggerInterface|null $logger              Optional: PSR-3 Logger
41
     */
42
    public function __construct( \PDO $pdo, $pages_table, $pages_roles_table, LoggerInterface $logger = null )
43
    {
44
        // Prerequisites
45
        $this->pages_table       = $pages_table ?: $this->pages_table;
46
        $this->pages_roles_table = $pages_roles_table   ?: $this->pages_roles_table;
47
        $this->setLogger( $logger ?: new NullLogger );
48
49
        // Read pages and allowed roles
50
        $sql =  "SELECT
51
        Page.route,
52
        GROUP_CONCAT(Page_Roles.role_id SEPARATOR '{$this->separator}') AS roles
53
54
        FROM      {$this->pages_table} Page
55
        LEFT JOIN {$this->pages_roles_table}   Page_Roles
56
        ON Page.id = Page_Roles.page_id
57
58
        WHERE Page_Roles.role_id IS NOT NULL
59
        GROUP BY Page.route";
60
61
        // Prepare business
62
        $this->stmt = $pdo->prepare( $sql );
63
    }
64
65
66
    /**
67
     * @return array
68
     */
69
    public function __invoke( )
70
    {
71
        $this->stmt->execute();
72
73
        // Fetch results; explode arrays
74
        $acl = array_map(function( $roles ) {
75
            return array_filter(explode($this->separator, $roles));
76
        }, $this->stmt->fetchAll( \PDO::FETCH_UNIQUE | \PDO::FETCH_OBJ | \PDO::FETCH_COLUMN));
77
78
        return $acl;
79
    }
80
}
81