Completed
Push — master ( 474113...884aa1 )
by Carsten
02:20
created

PdoAllWebsites   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 57
ccs 14
cts 19
cp 0.7368
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 42 6
1
<?php
2
namespace Germania\Websites;
3
4
class PdoAllWebsites extends Websites implements WebsitesInterface
5
{
6
7
    /**
8
     * @var string
9
     */
10
    public $table = 'pages';
11
12
    /**
13
     * @param PDO             $pdo
14
     * @param WebsiteAbstract $website  Optional: Website template object
15
     * @param string          $table    Optional: Websites table name
16
     */
17 1
    public function __construct( \PDO $pdo, WebsiteAbstract $website = null, $table = null  )
18
    {
19 1
        $this->table = $table ?: $this->table;
20
21
        // ID is listed twice here in order to use it with FETCH_UNIQUE as array key
22
        $sql = "SELECT
23
        id,
24
        id,
25
        title,
26
        route,
27
        content_file,
28
        template,
29
        dom_id,
30
        javascripts,
31
        stylesheets,
32
        is_active
33
34 1
        FROM {$this->table}
35
36 1
        WHERE 1";
37
38 1
        $stmt = $pdo->prepare( $sql );
39
40 1
        $stmt->setFetchMode( \PDO::FETCH_CLASS, $website ? get_class($website) : Website::class );
41
42 1
        if (!$stmt->execute()):
43
            throw new \RuntimeException("Could not retrieve Websites from database");
44
        endif;
45
46 1
        $this->websites = array_map(function($row) {
47
            // Cast numeric is_active field to integer
48 1
            $row->is_active = (int) $row->is_active;
49
            // Split into array
50 1
            if (isset($row->javascripts)):
51
                $row->javascripts = explode(",", $row->javascripts);
52
            endif;
53 1
            if (isset($row->stylesheets)):
54
                $row->stylesheets = explode(",", $row->stylesheets);
55
            endif;
56 1
            return $row;
57 1
        }, $stmt->fetchAll(\PDO::FETCH_UNIQUE));
58 1
    }
59
60
}
61
62