PdoAllCouponSheets   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 53
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 3
A count() 0 4 1
A getIterator() 0 4 1
1
<?php
2
namespace Germania\Coupons;
3
4
class PdoAllCouponSheets implements \Countable, \IteratorAggregate
5
{
6
7
    public $coupon_sheets = array();
8
9
    /**
10
     * @param \PDO   $pdo
11
     * @param string $sheets_table
12
     * @param string $coupons_table
13
     * @param string $php_class
14
     */
15
    public function __construct( \PDO $pdo, $sheets_table, $coupons_table, $php_class = null)
16
    {
17
        $sql = "SELECT
18
        Sheets.id,
19
        Sheets.slug,
20
        Sheets.name,
21
        Sheets.quantity,
22
        Sheets.valid_from,
23
        Sheets.valid_until,
24
        GROUP_CONCAT(Coupons.code) AS coupons
25
26
        FROM `{$sheets_table}` Sheets
27
28
        RIGHT JOIN `{$coupons_table}` Coupons
29
        ON Coupons.coupon_sheet_id = Sheets.id
30
31
        GROUP BY Sheets.id";
32
33
        $stmt = $pdo->prepare( $sql );
34
35
        if (!$stmt->execute()) {
36
            throw new \RuntimeException("Could not execute PDOStatement.");
37
        }
38
39
        $this->coupon_sheets = $stmt->fetchAll(\PDO::FETCH_CLASS, $php_class ?: CouponSheet::class);
40
    }
41
42
43
    public function count()
44
    {
45
        return count($this->coupon_sheets);
46
    }
47
48
49
    /**
50
     * @return ArrayIterator
51
     */
52
    public function getIterator()
53
    {
54
        return new \ArrayIterator( $this->coupon_sheets);
55
    }
56
}
57
58