Completed
Push — master ( bef18b...326c30 )
by Carsten
08:15 queued 04:08
created

PdoAllSalesmen   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.67%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 123
ccs 23
cts 30
cp 0.7667
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __debugInfo() 0 7 1
A __construct() 0 37 3
A has() 0 6 2
A get() 0 12 3
A getIterator() 0 4 1
A count() 0 4 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 40
    public function __construct (\PDO $pdo, $table, SalesmanInterface $salesman = null)
37
    {
38 40
        $this->table    = $table;
39 40
        $this->pdo      = $pdo;
40 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 40
        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 40
        $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 40
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_salesman_class );
65
66 40
        if (!$this->stmt->execute()):
67
            throw new SalesmanDatabaseException("PdoAllSalesmen: Could not execute SQL query");
68
        endif;
69
70 40
        $this->salesmen = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE);
71
72 40
    }
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 16
    public function has ($aussendienst_nummer) {
90 16
        if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) {
91
            $aussendienst_nummer = $aussendienst_nummer->getSalesmanId();
92
        }
93 16
        return array_key_exists($aussendienst_nummer, $this->salesmen);
94
    }
95
96
97
    /**
98
     * @implements ContainerInterface
99
     */
100 16
    public function get ($aussendienst_nummer) {
101 16
        if ($aussendienst_nummer instanceOf SalesmanIdProviderInterface) {
102
            $aussendienst_nummer = $aussendienst_nummer->getSalesmanId();
103
        }
104
105 16
        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 8
        return $this->salesmen[$aussendienst_nummer];
111
    }
112
113
114
    /**
115
     * @return Iterator
116
     */
117 28
    public function getIterator()
118
    {
119 28
        return new \ArrayIterator( $this->salesmen );
120
    }
121
122
123
    /**
124
     * @return int
125
     */
126 8
    public function count()
127
    {
128 8
        return count( $this->salesmen );
129
    }
130
}
131