DbalMapper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 100
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
doInsert() 0 1 ?
doLoad() 0 1 ?
A __construct() 0 5 1
A abstractFind() 0 17 3
A abstractInsert() 0 12 1
A clean() 0 4 1
A findStatement() 0 3 1
A insertStatement() 0 3 1
A load() 0 13 2
A loadAll() 0 9 2
A setPropertyValue() 0 8 1
1
<?php
2
3
namespace Scheduler\Infrastructure\DBAL;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Statement;
7
8
abstract class DbalMapper
9
{
10
    protected $db;
11
    protected $loadedMap;
12
13
    public function __construct(Connection $db)
14
    {
15
        $this->db = $db;
16
        $this->loadedMap = [];
17
    }
18
19
    protected function abstractFind($id)
20
    {
21
        if (isset($this->loadedMap[$id])) {
22
            return $this->loadedMap[$id];
23
        }
24
25
        $findStatement = $this->db->prepare($this->findStatement());
26
        $findStatement->bindValue(1, $id, \PDO::PARAM_INT);
27
        $findStatement->execute();
28
        $resultSet = $findStatement->fetch();
29
30
        if (! $resultSet) {
31
            return null;
32
        }
33
34
        return $this->load($resultSet);
35
    }
36
37
    protected function abstractInsert($subject)
38
    {
39
        $insertStatement = $this->db->prepare(static::insertStatement());
40
        $this->doInsert($subject, $insertStatement);
0 ignored issues
show
Compatibility introduced by
$insertStatement of type object<Doctrine\DBAL\Driver\Statement> is not a sub-type of object<Doctrine\DBAL\Statement>. It seems like you assume a concrete implementation of the interface Doctrine\DBAL\Driver\Statement to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
41
42
        $id = (int) $this->db->lastInsertId();
43
        $this->setPropertyValue($subject, "id", $id);
44
45
        $this->loadedMap[$id] = $subject;
46
47
        return $id;
48
    }
49
50
    public function clean()
51
    {
52
        $this->loadedMap = [];
53
    }
54
55
    /**
56
     * Bind query parameters and execute insert statement
57
     *
58
     * @param            $subject
59
     * @param  Statement $insertStatement
60
     *
61
     * @return int                        The id of the inserted row
62
     */
63
    abstract protected function doInsert($subject, Statement $insertStatement);
64
65
    abstract protected function doLoad($id, array $resultSet);
66
67
    protected static function findStatement()
68
    {
69
    }
70
71
    protected static function insertStatement()
72
    {
73
    }
74
75
    protected function load(array $resultSet)
76
    {
77
        $id = $resultSet["id"];
78
79
        if (isset($this->loadedMap[$id])) {
80
            return $this->loadedMap[$id];
81
        }
82
83
        $result = $this->doLoad($id, $resultSet);
84
        $this->loadedMap[$id] = $result;
85
86
        return $result;
87
    }
88
89
    protected function loadAll(Statement $statement)
90
    {
91
        $result = [];
92
        foreach ($statement as $row) {
93
            $result[] = $this->load($row);
94
        }
95
96
        return $result;
97
    }
98
99
    protected function setPropertyValue($subject, $name, $value)
100
    {
101
        $reflector = new \ReflectionClass($subject);
102
103
        $property = $reflector->getProperty($name);
104
        $property->setAccessible(true);
105
        $property->setValue($subject, $value);
106
    }
107
}
108