PdoValidAtDateTimeCouponSheetsFactory::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
namespace Germania\Coupons;
3
4
class PdoValidAtDateTimeCouponSheetsFactory
5
{
6
7
    /**
8
     * @var PDOStatement
9
     */
10
    public $stmt;
11
12
13
    /**
14
     * @var string
15
     */
16
    public $php_class;
17
18
19
    /**
20
     * @param \PDO   $pdo
21
     * @param string $sheets_table
22
     * @param string $coupons_table
23
     * @param string $php_class
24
     */
25
    public function __construct( \PDO $pdo, $sheets_table, $coupons_table, $php_class = null)
26
    {
27
        $this->php_class = $php_class ?: CouponSheet::class;
28
29
        $sql = "SELECT
30
        Sheets.id,
31
        Sheets.slug,
32
        Sheets.name,
33
        Sheets.quantity,
34
        Sheets.valid_from,
35
        Sheets.valid_until,
36
        GROUP_CONCAT(Coupons.code) AS coupons
37
38
        FROM `{$sheets_table}` Sheets
39
40
        RIGHT JOIN `{$coupons_table}` Coupons
41
        ON Coupons.coupon_sheet_id = Sheets.id
42
43
        WHERE (valid_until IS NULL OR valid_until >= :datetime_string)
44
        AND   (valid_from  <= :datetime_string)
45
46
        GROUP BY Sheets.id";
47
48
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type object<PDOStatement> is incompatible with the declared type object<Germania\Coupons\PDOStatement> of property $stmt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
    }
50
51
52
    /**
53
     * @return ArrayIterator
54
     */
55
    public function __invoke( \DateTimeInterface $when, $php_class = null )
56
    {
57
        if (!$this->stmt->execute([
58
            'datetime_string' => $when->format("Y-m-d H:i:s")
59
        ])) {
60
            throw new \RuntimeException("Could not execute PDOStatement.");
61
        }
62
63
64
        $sheets = $this->stmt->fetchAll(\PDO::FETCH_CLASS, $php_class ?: $this->php_class);
65
66
        return new \ArrayIterator( $sheets );
67
    }
68
69
}
70
71