1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opensoft\Rollout\Storage; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Storage adapter using PDO |
7
|
|
|
* |
8
|
|
|
* @author Baldur Rensch <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class PDOStorageAdapter implements StorageInterface |
11
|
|
|
{ |
12
|
|
|
const STMT_SELECT = 'SELECT settings FROM :table WHERE name = :key'; |
13
|
|
|
const STMT_INSERT = 'INSERT INTO :table (name, settings) VALUES (:key, :value)'; |
14
|
|
|
const STMT_UPDATE = 'UPDATE :table SET settings = :value WHERE name = :key'; |
15
|
|
|
const STMT_DELETE = 'DELETE FROM :table WHERE name = :key'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \PDO |
19
|
|
|
*/ |
20
|
|
|
private $pdoConnection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $tableName; |
26
|
|
|
|
27
|
|
|
public function __construct(\PDO $pdoConnection, $tableName = 'rollout_feature') |
28
|
|
|
{ |
29
|
|
|
$this->pdoConnection = $pdoConnection; |
30
|
|
|
$this->tableName = $tableName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function get($key) |
37
|
|
|
{ |
38
|
|
|
$statement = $this->pdoConnection->prepare($this->getSQLStatement(self::STMT_SELECT)); |
39
|
|
|
|
40
|
|
|
$statement->bindParam('key', $key); |
41
|
|
|
$statement->setFetchMode(\PDO::FETCH_ASSOC); |
42
|
|
|
|
43
|
|
|
$statement->execute(); |
44
|
|
|
|
45
|
|
|
$result = $statement->fetch(); |
46
|
|
|
|
47
|
|
|
if (false === $result) { |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $result['settings']; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function set($key, $value) |
58
|
|
|
{ |
59
|
|
|
if (null === $this->get($key)) { |
60
|
|
|
$sql = self::STMT_INSERT; |
61
|
|
|
} else { |
62
|
|
|
$sql = self::STMT_UPDATE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$statement = $this->pdoConnection->prepare($this->getSQLStatement($sql)); |
66
|
|
|
|
67
|
|
|
$statement->bindParam('key', $key); |
68
|
|
|
$statement->bindParam('value', $value); |
69
|
|
|
|
70
|
|
|
$statement->execute(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $key |
75
|
|
|
*/ |
76
|
|
|
public function remove($key) |
77
|
|
|
{ |
78
|
|
|
$statement = $this->pdoConnection->prepare($this->getSQLStatement(self::STMT_DELETE)); |
79
|
|
|
|
80
|
|
|
$statement->bindParam('key', $key); |
81
|
|
|
$statement->execute(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $sql |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
private function getSQLStatement($sql) |
90
|
|
|
{ |
91
|
|
|
return str_replace(':table', $this->tableName, $sql); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|