Completed
Push — master ( bd69b1...b47d34 )
by nicolas
05:24 queued 02:30
created

PdoScriptCache::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Dekalee\AdbackAnalytics\Driver;
4
5
/**
6
 * Class PdoScriptCache
7
 */
8
class PdoScriptCache extends SqlScriptCache implements ScriptCacheInterface
9
{
10
    protected $connection;
11
12
    /**
13
     * @param \PDO $connection
14
     */
15
    public function __construct(\PDO $connection)
16
    {
17
        $this->connection = $connection;
18
    }
19
20
    /**
21
     * @param string $key
22
     *
23
     * @return string|null
24
     */
25
    protected function get($key)
26
    {
27
        $request = $this->connection->prepare('SELECT our_value FROM adback_cache_table WHERE our_key = :key LIMIT 1');
28
        $request->execute([
29
            'key' => $key,
30
        ]);
31
32
        $data = $request->fetch();
33
        $request->closeCursor();
34
        if (is_array($data) && array_key_exists('our_value', $data)) {
35
            return $data['our_value'];
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * @param string $key
43
     * @param string $value
44
     */
45 View Code Duplication
    protected function set($key, $value)
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...
46
    {
47
        $this->clear($key);
48
        $request = $this->connection->prepare("INSERT INTO adback_cache_table (our_key, our_value) VALUES (:key, :value)");
49
        $request->execute([
50
            'key' => $key,
51
            'value' => $value,
52
        ]);
53
    }
54
55
    /**
56
     * @param string $key
57
     */
58
    protected function clear($key)
59
    {
60
        $request = $this->connection->prepare("DELETE FROM adback_cache_table WHERE our_key = :key");
61
        $request->execute([
62
            'key' => $key,
63
        ]);
64
    }
65
}
66