Test Failed
Branch lab/data (a414ec)
by Gabor
07:41
created

SqlQueryAdapter::getQueryIdentifierList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Query\SQL;
15
16
use InvalidArgumentException;
17
use PDO;
18
use RuntimeException;
19
use WebHemi\Data\Driver\DriverInterface;
20
use WebHemi\Data\Query\QueryInterface;
21
22
/**
23
 * Class SqlQueryAdapter
24
 */
25
class SqlQueryAdapter implements QueryInterface
26
{
27
    /**
28
     * @var PDO
29
     */
30
    private $driverAdapter;
31
32
    /**
33
     * @var array
34
     */
35
    private $identifierList = [];
36
37
    /**
38
     * QueryInterface constructor.
39
     *
40
     * @param DriverInterface $driverAdapter
41
     */
42
    public function __construct(DriverInterface $driverAdapter)
43
    {
44
        $this->driverAdapter = $driverAdapter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $driverAdapter of type object<WebHemi\Data\Driver\DriverInterface> is incompatible with the declared type object<PDO> of property $driverAdapter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        $this->init();
46
    }
47
48
    /**
49
     * Collects all the valid statements.
50
     */
51
    private function init() : void
52
    {
53
        $statementFiles = glob(__DIR__.'/statements/*.sql', GLOB_BRACE);
54
55
        foreach ($statementFiles as $file) {
56
            $this->identifierList[basename($file, '.sql')] = $file;
57
        }
58
    }
59
60
    /**
61
     * Returns the Data Driver instance.
62
     *
63
     * @return DriverInterface
64
     */
65
    public function getDriver() : DriverInterface
66
    {
67
        return $this->driverAdapter;
68
    }
69
70
    /**
71
     * Returns all the query identifiers assigned to the query adapter.
72
     *
73
     * @return array
74
     */
75
    public function getQueryIdentifierList() : array
76
    {
77
        return $this->identifierList;
78
    }
79
80
    /**
81
     * Fetches data buy executing a query identified by ID.
82
     *
83
     * @param string $queryIdentifier
84
     * @param array $parameters
85
     * @throws InvalidArgumentException
86
     * @throws RuntimeException
87
     * @return null|array
88
     */
89
    public function fetchData(string $queryIdentifier, array $parameters = []) : ? array
90
    {
91
        $data = null;
92
93
        if (!isset($this->identifierList[$queryIdentifier])) {
94
            throw new InvalidArgumentException(
95
                sprintf('No such query found for this adapter: "%s"', $queryIdentifier),
96
                1000
97
            );
98
        }
99
100
        $query = file_get_contents($this->identifierList[$queryIdentifier]);
101
102
        $statement = $this->driverAdapter->prepare($query);
103
104
        foreach ($parameters as $parameter => $value) {
105
            $statement->bindValue($parameter, $value, $this->getValueType($value));
106
        }
107
108
        try {
109
            $executedSuccessful = $statement->execute();
110
        } catch (\Throwable $exception) {
111
            throw new RuntimeException(
112
                sprintf('Error executing query for "%s". %s', $queryIdentifier, $exception->getMessage()),
113
                1000
114
            );
115
        }
116
117
        if (!$executedSuccessful) {
118
            throw new RuntimeException(
119
                sprintf('Error running query: "%s"', $queryIdentifier),
120
                1001
121
            );
122
        }
123
124
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
125
126
        if (is_array($result)) {
127
            $data = $result;
128
        }
129
130
        return $data;
131
    }
132
133
    /**
134
     * Returns the PDO type of the value.
135
     *
136
     * @param mixed $value
137
     * @return int
138
     */
139
    private function getValueType($value) : int
140
    {
141
        $type = PDO::PARAM_STR;
142
143
        if (is_numeric($value)) {
144
            $type = PDO::PARAM_INT;
145
        } elseif (is_null($value)) {
146
            $type = PDO::PARAM_NULL;
147
        } elseif (is_bool($value)) {
148
            $type = PDO::PARAM_BOOL;
149
        }
150
151
        return $type;
152
    }
153
}
154