PdoPrices::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.9332
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace Germania\Prices;
3
4
use Interop\Container\ContainerInterface;
5
6
7
class PdoPrices implements PricesInterface
8
{
9
    public $table = 'germania_prices';
10
11
    public $pdo;
12
    public $pdo_stmt;
13
14
    /**
15
     * @param \PDO   $pdo   PDO Handler
16
     * @param string $table Prices table name, default: "germania_prices"
17
     *
18
     * @uses  $table
19
     */
20
    public function __construct( \PDO $pdo, $table = null)
21
    {
22
        $this->pdo = $pdo;
23
24
        $table = $table ?: $this->table;
25
26
        $sql = "SELECT
27
        amount,
28
        price,
29
        description
30
31
        FROM $table
32
        WHERE article_id = :id";
33
34
        $this->pdo_stmt = $pdo->prepare( $sql );
35
    }
36
37
    /**
38
     * @param  mixed $article_id
39
     * @return array
40
     */
41
    public function __invoke( $article_id )
42
    {
43
        try {
44
            return $this->get( $article_id );
45
        }
46
        catch (NoPriceFoundException $e) {
47
            return array();
48
        }
49
50
    }
51
52
    /**
53
     * @param  mixed $article_id
54
     * @return boolean
55
     */
56
    public function has( $article_id )
57
    {
58
        $this->pdo_stmt->execute([
59
            'table' => static::$table,
60
            'id' => $article_id
61
        ]);
62
        return (bool) $this->pdo_stmt->fetchColumn();
63
    }
64
65
66
    /**
67
     * @param  mixed $article_id
68
     * @return array
69
     */
70
    public function get( $article_id )
71
    {
72
        $this->pdo_stmt->execute([
73
            'id' => $article_id
74
        ]);
75
76
        if ($prices = $this->pdo_stmt->fetchAll( \PDO::FETCH_OBJ )) {
77
            return $prices;
78
        }
79
        throw new NoPriceFoundException;
80
    }
81
}
82