Completed
Push — master ( d1d08c...570c1f )
by Carsten
11:09 queued 05:42
created

PdoAllSalesmen::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Germania\Salesmen;
3
4
use Psr\Container\ContainerInterface;
5
use Germania\Salesmen\Exceptions\SalesmanNotFoundException;
6
use Germania\Salesmen\Exceptions\SalesmanDatabaseException;
7
8
class PdoAllSalesmen implements ContainerInterface, \IteratorAggregate, \Countable
9
{
10
    /**
11
     * @var \PDO
12
     */
13
    public $pdo;
14
15
    /**
16
     * @var string
17
     */
18
    public $php_salesman_class;
19
20
    /**
21
     * @var string
22
     */
23
    public $table = "salesmen";
24
25
    /**
26
     * Holds all retrieved SalesmenInterface instances
27
     * @var SalesmanInterface[]
28
     */
29
    public $salesmen = array();
30
31
    /**
32
     * @param \PDO                   $pdo      PDO handler
33
     * @param string                 $table    The table name to use
34
     * @param SalesmanInterface|null $salesman Class or object template to work with
35
     */
36
    public function __construct (\PDO $pdo, $table, SalesmanInterface $salesman = null)
37
    {
38
        $this->table    = $table;
39
        $this->pdo      = $pdo;
40
        $this->php_salesman_class = $salesman ? get_class($salesman) : Salesman::class;
41
42
        // aussendienst_nummer ID is listed twice here in order to use it with FETCH_UNIQUE as array key
43
        $sql = "SELECT
44
        aussendienst_nummer,
45
        aussendienst_nummer          AS salesman_id,
46
        aussendienst_vorname         AS first_name,
47
        aussendienst_nachname        AS last_name,
48
        aussendienst_retailer_number AS retailer_number,
49
        aussendienst_email           AS email,
50
        user_id,
51
        is_active
52
53
        FROM {$this->table}
54
55
        WHERE 1
56
57
        ORDER BY
58
        aussendienst_nachname ASC,
59
        aussendienst_vorname ASC,
60
        aussendienst_nummer ASC";
61
62
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Bug introduced by
The property stmt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
64
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_salesman_class );
65
66
        if (!$this->stmt->execute()):
67
            throw new SalesmanDatabaseException("PdoAllSalesmen: Could not execute SQL query");
68
        endif;
69
70
        $this->salesmen = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE);
71
72
    }
73
74
75
    /**
76
     * @return array
77
     */
78
    public function __debugInfo() {
79
        return [
80
            'DatabaseTable'    => $this->table,
81
            'NumberOfSalesmen' => $this->count(),
82
            'SalesmanClass'    => $this->php_salesman_class
83
        ];
84
    }
85
86
    /**
87
     * @implements ContainerInterface
88
     */
89
    public function has ($aussendienst_nummer) {
90
        if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) {
91
            $aussendienst_nummer = $aussendienst_nummer->getSalesmanId();
92
        }
93
        return array_key_exists($aussendienst_nummer, $this->salesmen);
94
    }
95
96
97
    /**
98
     * @implements ContainerInterface
99
     */
100
    public function get ($aussendienst_nummer) {
101
        if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) {
102
            $aussendienst_nummer = $aussendienst_nummer->getSalesmanId();
103
        }
104
105
        if (!$this->has($aussendienst_nummer)) {
106
            $msg = sprintf("Could not find Salesman with ADM-Nummer '%s'", $aussendienst_nummer);
107
            throw new SalesmanNotFoundException( $msg );
108
        }
109
110
        return $this->salesmen[$aussendienst_nummer];
111
    }
112
113
114
    /**
115
     * @return Iterator
116
     */
117
    public function getIterator()
118
    {
119
        return new \ArrayIterator( $this->salesmen );
120
    }
121
122
123
    /**
124
     * @return int
125
     */
126
    public function count()
127
    {
128
        return count( $this->salesmen );
129
    }
130
}
131