Test Setup Failed
Push — master ( 8f6b0a...c03d3f )
by Carsten
04:27 queued 12s
created

PdoActiveSalesmen::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace Germania\Salesmen;
3
4
use Psr\Container\ContainerInterface;
5
6
class PdoActiveSalesmen implements ContainerInterface, \IteratorAggregate, \Countable
7
{
8
    /**
9
     * @var \PDO
10
     */
11
    public $pdo;
12
13
    public $table = "salesmen";
14
15
    public $salesmen = array();
16
17
    public function __construct (\PDO $pdo, $table, SalesmanInterface $salesman = null)
18
    {
19
        $this->table    = $table;
20
        $this->pdo      = $pdo;
21
22
        // aussendienst_nummer ID is listed twice here in order to use it with FETCH_UNIQUE as array key
23
        $sql = "SELECT
24
        aussendienst_nummer,
25
        aussendienst_nummer as salesman_id,
26
        aussendienst_vorname as first_name,
27
        aussendienst_nachname as last_name,
28
        aussendienst_retailer_number as retailer_number,
29
        aussendienst_email as email,
30
        user_id,
31
        is_active
32
33
        FROM {$this->table}
34
35
        WHERE is_active > 0
36
37
        ORDER BY
38
        aussendienst_nachname ASC,
39
        aussendienst_vorname ASC,
40
        aussendienst_nummer ASC";
41
42
        $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...
43
44
        $this->stmt->setFetchMode( \PDO::FETCH_CLASS, $salesman ? get_class($salesman) : Salesman::class );
45
46
        $bool = $this->stmt->execute();
0 ignored issues
show
Unused Code introduced by
$bool is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
48
        $this->salesmen = $this->stmt->fetchAll( \PDO::FETCH_UNIQUE);
49
    }
50
51
52
    /**
53
     * @implements ContainerInterface
54
     */
55
    public function has ($aussendienst_nummer) {
56
        return array_key_exists($aussendienst_nummer, $this->salesmen);
57
    }
58
59
60
    /**
61
     * @implements ContainerInterface
62
     */
63
    public function get ($aussendienst_nummer) {
64
        if (!$this->has($aussendienst_nummer)) {
65
            $msg = sprintf("Could not find Salesman with ADM-Nummer '%s'", $aussendienst_nummer);
66
            throw new SalesmanNotFoundException( $msg );
67
        }
68
69
        return $this->salesmen[$aussendienst_nummer];
70
    }
71
72
73
    /**
74
     * @return Iterator
75
     */
76
    public function getIterator()
77
    {
78
        return new \ArrayIterator( $this->salesmen );
79
    }
80
81
82
    /**
83
     * @return int
84
     */
85
    public function count()
86
    {
87
        return count( $this->salesmen );
88
    }
89
}
90