Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

ConnectorAdapter::updateData()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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