Passed
Push — master ( 749b16...167cfa )
by Gabor
04:47
created

SQLiteAdapter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
A saveData() 0 4 2
B insertData() 0 26 3
B updateData() 0 24 3
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 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\Adapter\Data\PDO;
15
16
use InvalidArgumentException;
17
use RuntimeException;
18
use WebHemi\Adapter\Data\DataDriverInterface;
19
20
/**
21
 * Class SQLiteAdapter.
22
 *
23
 * @codeCoverageIgnore - Used by unit test only.
24
 */
25
class SQLiteAdapter extends MySQLAdapter
26
{
27
    /**
28
     * SQLiteAdapter constructor.
29
     *
30
     * @param DataDriverInterface $dataDriver
31
     * @throws InvalidArgumentException
32
     */
33
    public function __construct(DataDriverInterface $dataDriver)
34
    {
35
        if (!$dataDriver instanceof SQLiteDriver) {
36
            $type = gettype($dataDriver);
37
38
            if ($type == 'object') {
39
                $type = get_class($dataDriver);
40
            }
41
42
            $message = sprintf(
43
                'Can\'t create %s instance. The parameter must be an instance of SQLiteDriver, %s given.',
44
                __CLASS__,
45
                $type
46
            );
47
48
            throw new InvalidArgumentException($message, 1001);
49
        }
50
51
        $this->dataDriver = $dataDriver;
52
    }
53
54
    /**
55
     * Insert or update entity in the storage.
56
     *
57
     * @param int $identifier
58
     * @param array $data
59
     * @throws RuntimeException
60
     * @return int The ID of the saved entity in the storage
61
     */
62
    public function saveData(?int $identifier = null, array $data) : int
63
    {
64
        return empty($identifier) ? $this->insertData($data) : $this->updateData($identifier, $data);
65
    }
66
67
    /**
68
     * Insert data.
69
     *
70
     * @param array $data
71
     * @return int
72
     */
73
    protected function insertData(array $data) : int
74
    {
75
        $query = "INSERT INTO {$this->dataGroup}";
76
77
        $queryColumns = [];
78
        $queryData = [];
79
        $queryBind = [];
80
81
        foreach ($data as $fieldName => $value) {
82
            $queryColumns[] = $fieldName;
83
            $queryData[] = "?";
84
            $queryBind[] = $value;
85
        }
86
87
        $query .= ' ('.implode(', ', $queryColumns).') VALUES ('.implode(', ', $queryData).')';
88
89
        $statement = $this->dataDriver->prepare($query);
90
91
        if (!$statement) {
92
            throw new RuntimeException('Query error', 1002);
93
        }
94
        $this->bindValuesToStatement($statement, $queryBind);
95
        $statement->execute();
96
97
        return (int)$this->dataDriver->lastInsertId();
98
    }
99
100
    /**
101
     * Update data.
102
     *
103
     * @param int $identifier
104
     * @param array $data
105
     * @return int
106
     */
107
    protected function updateData(int $identifier, array $data) : int
108
    {
109
        $query = "UPDATE {$this->dataGroup}";
110
111
        $queryData = [];
112
        $queryBind = [];
113
114
        foreach ($data as $fieldName => $value) {
115
            $queryData[] = "{$fieldName} = ?";
116
            $queryBind[] = $value;
117
        }
118
119
        $query .= ' SET '.implode(', ', $queryData)." WHERE {$this->idKey} = ?";
120
        $queryBind[] = $identifier;
121
        $statement = $this->dataDriver->prepare($query);
122
123
        if (!$statement) {
124
            throw new RuntimeException('Query error', 1003);
125
        }
126
        $this->bindValuesToStatement($statement, $queryBind);
127
        $statement->execute();
128
129
        return $identifier;
130
    }
131
}
132