SphinxAdapter::updateToDatabase()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 19
nc 1
nop 3
1
<?php
2
3
namespace NilPortugues\Cache\Adapter;
4
5
use DateTime;
6
use NilPortugues\Cache\Adapter\SQL\AbstractAdapter;
7
use PDO;
8
use PDOException;
9
10
/**
11
 * Class SphinxAdapter
12
 * @package NilPortugues\Cache\Adapter
13
 */
14
class SphinxAdapter extends AbstractAdapter
15
{
16
    const SPHINX_ID_PLACEHOLDER = ':sphinx_id';
17
18
    /**
19
     * @var string
20
     */
21
    protected $connectionClass = 'NilPortugues\Cache\Adapter\SQL\Connection\SphinxPDOConnection';
22
23
    /**
24
     * @var array
25
     */
26
    protected $requiredKeys = [];
27
28
29
    /**
30
     * @param     $key
31
     * @param     $value
32
     * @param int $ttl
33
     *
34
     * @return $this
35
     */
36
    protected function insertToDatabase($key, $value, $ttl = 0)
37
    {
38
        $value = $this->storageDataStructure($value);
39
40
        $calculatedTtl = $this->fromDefaultTtl($ttl);
41
        $calculatedTtl = new DateTime(\date('Y-m-d H:i:s', $this->getCalculatedTtl($calculatedTtl)));
42
43
        $databaseValue = $this->getFromDatabase($key);
44
        if (false === empty($databaseValue)) {
45
            $databaseValue[self::CACHE_VALUE] = $value;
46
            $this->updateToDatabase($key, $databaseValue, $calculatedTtl);
47
            return $this;
48
        }
49
50
        $stmt = $this->connection->prepare(
51
            \sprintf(
52
                'INSERT INTO %s(id,%s,%s,%s) VALUES(%s,%s,%s,%s)',
53
                $this->cacheTableName,
54
                self::TABLE_CACHE_ID,
55
                self::TABLE_CACHE_VALUE,
56
                self::TABLE_CACHE_TTL,
57
                self::SPHINX_ID_PLACEHOLDER,
58
                self::QUERY_ID_PLACEHOLDER,
59
                self::QUERY_VALUE_PLACEHOLDER,
60
                self::QUERY_TTL_PLACEHOLDER
61
            )
62
        );
63
64
        $sphinxId = (int) $this->connection->lastInsertId()+1;
65
66
        $stmt->bindParam(self::SPHINX_ID_PLACEHOLDER, $sphinxId, PDO::PARAM_STR);
67
        $stmt->bindParam(self::QUERY_ID_PLACEHOLDER, $key, PDO::PARAM_STR);
68
        $stmt->bindParam(self::QUERY_VALUE_PLACEHOLDER, $value, PDO::PARAM_STR);
69
        $calculatedTtl = $calculatedTtl->format('Y-m-d H:i:s');
70
        $stmt->bindParam(self::QUERY_TTL_PLACEHOLDER, $calculatedTtl, PDO::PARAM_STR);
71
        $stmt->execute();
72
73
        return $this;
74
    }
75
76
77
    /**
78
     * @param $key
79
     * @param $value
80
     * @param DateTime $ttl
81
     * @return $this
82
     */
83
    protected function updateToDatabase($key, $value, DateTime $ttl)
84
    {
85
        $stmt = $this->connection->prepare(
86
            \sprintf(
87
                'REPLACE INTO %s(id, %s, %s, %s) VALUES(%s, %s, %s, %s)',
88
                $this->cacheTableName,
89
                self::TABLE_CACHE_ID,
90
                self::TABLE_CACHE_VALUE,
91
                self::TABLE_CACHE_TTL,
92
                self::SPHINX_ID_PLACEHOLDER,
93
                self::QUERY_ID_PLACEHOLDER,
94
                self::QUERY_VALUE_PLACEHOLDER,
95
                self::QUERY_TTL_PLACEHOLDER
96
            )
97
        );
98
99
        $stmt->bindParam(self::SPHINX_ID_PLACEHOLDER, $value['id'], PDO::PARAM_STR);
100
        $stmt->bindParam(self::QUERY_ID_PLACEHOLDER, $key, PDO::PARAM_STR);
101
        $stmt->bindParam(self::QUERY_VALUE_PLACEHOLDER, $value[self::CACHE_VALUE], PDO::PARAM_STR);
102
        $calculatedTtl = $ttl->format('Y-m-d H:i:s');
103
        $stmt->bindParam(self::QUERY_TTL_PLACEHOLDER, $calculatedTtl, PDO::PARAM_STR);
104
        $stmt->execute();
105
106
        return $this;
107
    }
108
109
110
    /**
111
     * @param string $key
112
     *
113
     * @return array
114
     */
115 View Code Duplication
    protected function getFromDatabase($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        try {
118
            $stmt = $this->connection->prepare(
119
                \sprintf(
120
                    'SELECT id, %s, %s FROM %s WHERE %s = %s',
121
                    self::TABLE_CACHE_VALUE,
122
                    self::TABLE_CACHE_TTL,
123
                    $this->cacheTableName,
124
                    self::TABLE_CACHE_ID,
125
                    self::QUERY_ID_PLACEHOLDER
126
                )
127
            );
128
129
            $stmt->bindParam(self::QUERY_ID_PLACEHOLDER, $key, PDO::PARAM_STR);
130
            $stmt->execute();
131
132
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
133
            return (\is_bool($result)) ? [] : $result;
134
        } catch (PDOException $e) {
135
            return [];
136
        }
137
    }
138
}
139