PdoAllSalesmen::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 64
    public function __construct (\PDO $pdo, $table, SalesmanInterface $salesman = null)
37
    {
38 64
        $this->table    = $table;
39 64
        $this->pdo      = $pdo;
40 64
        $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 64
        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 64
        $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 64
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_salesman_class );
65
66 64
        if (!$this->stmt->execute()):
67 8
            throw new SalesmanDatabaseException("PdoAllSalesmen: Could not execute SQL query");
68
        endif;
69
70 64
        $this->salesmen = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE);
71
72 64
    }
73
74
75
    /**
76
     * @return array
77
     */
78 8
    public function __debugInfo() {
79
        return [
80 8
            'DatabaseTable'    => $this->table,
81 8
            'NumberOfSalesmen' => $this->count(),
82 8
            'SalesmanClass'    => $this->php_salesman_class
83
        ];
84
    }
85
86
    /**
87
     * @implements ContainerInterface
88
     */
89 24
    public function has ($aussendienst_nummer) {
90 24
        if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) {
91 8
            $aussendienst_nummer = $aussendienst_nummer->getSalesmanId();
92
        }
93 24
        return array_key_exists($aussendienst_nummer, $this->salesmen);
94
    }
95
96
97
    /**
98
     * @implements ContainerInterface
99
     */
100 24
    public function get ($aussendienst_nummer) {
101 24
        if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) {
102 8
            $aussendienst_nummer = $aussendienst_nummer->getSalesmanId();
103
        }
104
105 24
        if (!$this->has($aussendienst_nummer)) {
106 8
            $msg = sprintf("Could not find Salesman with ADM-Nummer '%s'", $aussendienst_nummer);
107 8
            throw new SalesmanNotFoundException( $msg );
108
        }
109
110 16
        return $this->salesmen[$aussendienst_nummer];
111
    }
112
113
114
    /**
115
     * @return Iterator
116
     */
117 40
    public function getIterator()
118
    {
119 40
        return new \ArrayIterator( $this->salesmen );
120
    }
121
122
123
    /**
124
     * @return int
125
     */
126 16
    public function count()
127
    {
128 16
        return count( $this->salesmen );
129
    }
130
}
131