Completed
Pull Request — dev (#24)
by Ben
01:58
created

Pdo::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Benrowe\Laravel\Config\Storage;
4
5
use PDO;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Benrowe\Laravel\Config\Storage\PDO.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 * @package Benrowe\Laravel\Config\Storage
9
 */
10
class Pdo implements StorageInterface
11
{
12
    /**
13
     * @var PDO
14
     */
15
    private $pdo;
16
17
    /**
18
     * @var string
19
     */
20
    private $tableName;
21
22
    /**
23
     * @var array
24
     */
25
    private $sqlQueries = [
26
        'clear' => 'DELETE FROM %tablename%',
27
        'delete' => 'DELETE FROM %tablename% WHERE LOWER(LEFT(`key`, ?)) = ?',
28
        'load' => 'SELECT `key`, `value` FROM %tablename%',
29
        'save' => 'INSERT INTO %tablename% (`key`, `value`) VALUES (?,?)'
30
    ];
31
32
    /**
33
     * constructor
34
     *
35
     * @param PDO    $pdo        instance of pdo to execute the queries against
36
     * @param string $tableName  the table name to reference
37
     * @param array $sqlQueries custom queries
38
     */
39
    public function __construct(PDO $pdo, $tableName = 'config', array $sqlQueries = [])
40
    {
41
        $this->pdo = $pdo;
42
        $this->tableName = $tableName;
43
        $this->setSqlQueries($sqlQueries);
44
    }
45
    /**
46
     * Set the sql queries to be used
47
     *
48
     * @param array $queries additional queries to be used. Overrides the base
49
     *                       ones provided with this storage adapter
50
     */
51
    public function setSqlQueries(array $queries)
52
    {
53
        $this->sqlQueries = array_merge($this->sqlQueries, $queries);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function save($key, $value)
60
    {
61
        $this->delKey($key);
62
63
        if (is_array($value)) {
64
            foreach ($value as $i => $arrValue) {
65
                $this->saveKey($key.'['.$i.']', $arrValue);
66
            }
67
            return;
68
        }
69
70
        $this->saveKey($key, $value);
71
    }
72
73
    /**
74
     * Save the specific key into the database
75
     *
76
     * @param  string $key
77
     * @param  string|int|float $value
78
     */
79
    private function saveKey($key, $value)
80
    {
81
        $sql = $this->getSqlQuery('save');
82
        $stmt = $this->pdo->prepare($sql);
83
        $stmt->execute([$key, $value]);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function load()
90
    {
91
        $sql = $this->getSqlQuery('load');
92
        $stmt = $this->pdo->prepare($sql);
93
        $stmt->execute();
94
95
        $results = [];
96
        foreach ($stmt->fetchAll() as $row) {
97
            $results[$row['key']] = $row['value'];
98
        }
99
        return $results;
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function clear()
106
    {
107
        $this->pdo->exec($this->getSqlQuery('clear'));
108
    }
109
110
    /**
111
     * Generate the requested sql statement based on the provided name
112
     *
113
     * @param  string $name the query as referenced by it's name
114
     * @param  array  $vars any additional variables needed to generate the sql
115
     * @return string
116
     */
117
    private function getSqlQuery($name, array $vars = [])
118
    {
119
        $sql = $this->sqlQueries[$name];
120
        $vars = array_merge(['tablename' => $this->tablename], $vars);
0 ignored issues
show
Bug introduced by
The property tablename does not seem to exist. Did you mean tableName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
121
        foreach ($vars as $key => $value) {
122
            $sql = str_replace("%$key%", $value, $sql);
123
        }
124
125
        return $sql;
126
    }
127
128
    /**
129
     * Delete the requested key from the database storage
130
     *
131
     * @param  string $key
132
     */
133
    private function delKey($key)
134
    {
135
        $sql = $this->getSqlQuery('delete');
136
        $stmt = $this->pdo->prepare($sql);
137
        $params = [strlen($key), $key];
138
        $stmt->execute($params);
139
    }
140
}
141