Completed
Pull Request — master (#160)
by Lito
01:42
created

SessionPersistentDataStore::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace Kunnu\Dropbox\Store;
3
4
class SessionPersistentDataStore implements PersistentDataStoreInterface
5
{
6
7
    /**
8
     * Session Variable Prefix
9
     *
10
     * @var string
11
     */
12
    protected $prefix;
13
14
    /**
15
     * Create a new SessionPersistentDataStore instance
16
     *
17
     * @param string $prefix Session Variable Prefix
18
     */
19
    public function __construct($prefix = 'DBAPI_')
20
    {
21
        $this->prefix = $prefix;
22
        $this->start();
23
    }
24
25
    /**
26
     * Start session if not started previously
27
     */
28
    protected function start()
29
    {
30
        if (!isset($_SESSION)) {
31
            session_start();
32
        }
33
    }
34
35
    /**
36
     * Get a value from the store
37
     *
38
     * @param  string $key Data Key
39
     *
40
     * @return string|null
41
     */
42
    public function get($key)
43
    {
44
        if (isset($_SESSION[$this->prefix . $key])) {
45
            return $_SESSION[$this->prefix . $key];
46
        }
47
48
        return null;
49
    }
50
51
    /**
52
     * Set a value in the store
53
     * @param string $key   Data Key
54
     * @param string $value Data Value
55
     *
56
     * @return void
57
     */
58
    public function set($key, $value)
59
    {
60
        $_SESSION[$this->prefix . $key] = $value;
61
    }
62
63
    /**
64
     * Clear the key from the store
65
     *
66
     * @param $key Data Key
67
     *
68
     * @return void
69
     */
70
    public function clear($key)
71
    {
72
        if (isset($_SESSION[$this->prefix . $key])) {
73
            unset($_SESSION[$this->prefix . $key]);
74
        }
75
    }
76
}
77