ZendDbAdapterStorage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 80
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 10 2
A set() 0 11 2
A remove() 0 6 1
A getSQLStatement() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Adlogix package.
4
 *
5
 * (c) Allan Segebarth <[email protected]>
6
 * (c) Jean-Jacques Courtens <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Adlogix\Zf2Rollout\Storage;
13
14
15
use Opensoft\Rollout\Storage\StorageInterface;
16
use Zend\Db\Adapter\Adapter;
17
use Zend\Db\Adapter\ParameterContainer;
18
19
class ZendDbAdapterStorage implements StorageInterface
20
{
21
    const STMT_SELECT = 'SELECT settings FROM :table WHERE name = :key';
22
    const STMT_INSERT = 'INSERT INTO :table (name, settings) VALUES (:key, :value)';
23
    const STMT_UPDATE = 'UPDATE :table SET settings = :value WHERE name = :key';
24
    const STMT_DELETE = 'DELETE FROM :table WHERE name = :key';
25
26
    private $adapter;
27
28
    /**
29
     * @var string
30
     */
31
    private $tableName;
32
33
    /**
34
     * @param Adapter $adapter
35
     * @param string  $tableName
36
     */
37
    public function __construct(Adapter $adapter, $tableName)
38
    {
39
        $this->adapter = $adapter;
40
        $this->tableName = $tableName;
41
    }
42
43
    /**
44
     * @param  string $key
45
     *
46
     * @return string|null Null if the value is not found
47
     */
48
    public function get($key)
49
    {
50
        $statement = $this->adapter->createStatement($this->getSQLStatement(self::STMT_SELECT),
51
            new ParameterContainer(['key' => $key]));
52
        $resultSet = $statement->execute();
53
54
        $row = $resultSet->current();
55
56
        return (isset($row['settings'])) ? $row['settings'] : null;
57
    }
58
59
    /**
60
     * @param string $key
61
     * @param string $value
62
     *
63
     * @return void
64
     */
65
    public function set($key, $value)
66
    {
67
        if (null === $this->get($key)) {
68
            $sql = self::STMT_INSERT;
69
        } else {
70
            $sql = self::STMT_UPDATE;
71
        }
72
        $statement = $this->adapter->createStatement($this->getSQLStatement($sql),
73
            new ParameterContainer(['key' => $key, 'value' => $value]));
74
        $statement->execute();
75
    }
76
77
    /**
78
     * @param string $key
79
     *
80
     * @return void
81
     */
82
    public function remove($key)
83
    {
84
        $statement = $this->adapter->createStatement($this->getSQLStatement(self::STMT_DELETE),
85
            new ParameterContainer(['key' => $key]));
86
        $statement->execute();
87
    }
88
89
    /**
90
     * @param string $sql
91
     *
92
     * @return string
93
     */
94
    private function getSQLStatement($sql)
95
    {
96
        return str_replace(':table', $this->tableName, $sql);
97
    }
98
}