Db::getFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP;
3
4
use FMUP\Db\Factory;
5
use FMUP\Logger;
6
7
/**
8
 * Class Db
9
 * @package FMUP
10
 */
11
class Db implements Logger\LoggerInterface
12
{
13
    use Logger\LoggerTrait;
14
15
    protected $driver = Factory::DRIVER_PDO;
16
    protected $params = array();
17
    private $driverInstance = null;
18
    /**
19
     * @var Factory
20
     */
21
    private $factory;
22
23
    /**
24
     * @param array $params
25
     */
26 18
    public function __construct(array $params = array())
27
    {
28 18
        $this->driver = isset($params['db_driver']) ? $params['db_driver'] : Factory::DRIVER_PDO;
29 18
        $this->params = $params;
30 18
    }
31
32
    /**
33
     * @return Db\DbInterface|null
34
     * @throws Db\Exception
35
     */
36 1
    public function getDriver()
37
    {
38 1
        if (!is_null($this->driverInstance)) {
39 1
            return $this->driverInstance;
40
        }
41
42 1
        $driverInstance = $this->getFactory()->create($this->driver, $this->params);
43 1
        if ($driverInstance instanceof Logger\LoggerInterface && true === $this->hasLogger()) {
44 1
            $driverInstance->setLogger($this->getLogger());
45
        }
46
47 1
        $this->driverInstance = $driverInstance;
48 1
        return $this->driverInstance;
49
    }
50
51
    /**
52
     * @return Factory
53
     */
54 2
    public function getFactory()
55
    {
56 2
        if (!$this->factory) {
57 2
            $this->factory = Factory::getInstance();
58
        }
59 2
        return $this->factory;
60
    }
61
62
    /**
63
     * @param Factory $factory
64
     * @return $this
65
     */
66 1
    public function setFactory(Factory $factory)
67
    {
68 1
        $this->factory = $factory;
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param string $sql
74
     * @param array $params
75
     * @return bool
76
     */
77 1
    public function query($sql, array $params = array())
78
    {
79 1
        $statement = $this->getDriver()->prepare($sql);
80
81 1
        return $this->getDriver()->execute($statement, $params);
82
    }
83
84
    /**
85
     * @param string $sql
86
     * @param array $params
87
     * @return mixed
88
     * @throws Db\Exception
89
     * @deprecated use self::getIterator() instead
90
     */
91 1
    public function fetchAll($sql, array $params = array())
92
    {
93 1
        $statement = $this->getDriver()->prepare($sql);
94 1
        $this->getDriver()->execute($statement, $params);
95 1
        $arrayResult = $this->getDriver()->fetchAll($statement);
96 1
        return empty($arrayResult) ? array() : new \ArrayIterator($arrayResult);
97
    }
98
99
    /**
100
     * @param string $sql
101
     * @param array $params
102
     * @return array
103
     */
104 1
    public function fetchRow($sql, array $params = array())
105
    {
106 1
        $statement = $this->getDriver()->prepare($sql);
107 1
        $this->getDriver()->execute($statement, $params);
108
109 1
        return $this->getDriver()->fetchRow($statement);
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 1
    public function beginTransaction()
116
    {
117 1
        return $this->getDriver()->beginTransaction();
118
    }
119
120
    /**
121
     * @return bool
122
     */
123 1
    public function commit()
124
    {
125 1
        return $this->getDriver()->commit();
126
    }
127
128
    /**
129
     * @return bool
130
     */
131 1
    public function rollback()
132
    {
133 1
        return $this->getDriver()->rollback();
134
    }
135
136
    /**
137
     * @param string $name
138
     * @return string
139
     */
140 1
    public function lastInsertId($name = null)
141
    {
142 1
        return $this->getDriver()->lastInsertId($name);
143
    }
144
145
    /**
146
     * Force reconnection
147
     * @return Db\DbInterface
148
     */
149 1
    public function forceReconnect()
150
    {
151 1
        return $this->getDriver()->forceReconnect();
152
    }
153
154
    /**
155
     * Retrieve an iterator instead of array for data rows
156
     * @param string $sql
157
     * @param array $params
158
     * @return Db\FetchIterator
159
     */
160 2
    public function getIterator($sql, array $params = array())
161
    {
162 2
        return new Db\FetchIterator($this->getDriver()->prepare($sql), $this->getDriver(), $params);
0 ignored issues
show
Bug introduced by
It seems like $this->getDriver() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
163
    }
164
}
165