AbstractDriver::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: fsilva
5
 * Date: 02-11-2017
6
 * Time: 17:47
7
 */
8
9
namespace Slick\Http\Session\Driver;
10
11
use Slick\Http\Session\SessionDriverInterface;
12
13
/**
14
 * Class AbstractDriver
15
 * @package Slick\Http\Session\Driver
16
 */
17
abstract class AbstractDriver implements SessionDriverInterface
18
{
19
    /**
20
     * @var null|string
21
     */
22
    protected $domain = null;
23
24
    /**
25
     * @var int
26
     */
27
    protected $lifetime = 0;
28
29
    /**
30
     * @var string
31
     */
32
    protected $name = 'ID';
33
34
    /**
35
     * @var string
36
     */
37
    protected $prefix = 'slick_';
38
39
    /**
40
     * Creates a Session Driver
41
     *
42
     * @param array $options
43
     */
44
    public function __construct(array $options = [])
45
    {
46
        foreach ($options as $option => $value) {
47
            if (property_exists(__CLASS__, $option)) {
48
                $this->$option = $value;
49
            }
50
        }
51
    }
52
53
    /**
54
     * Returns the value store with provided key or the default value.
55
     *
56
     * @param string $key The key used to store the value in session.
57 10
     * @param string $default The default value if no value was stored.
58
     *
59 10
     * @return mixed The stored value or the default value if key
60 10
     *  was not found.
61 10
     */
62
    public function get($key, $default = null)
63
    {
64
        if (array_key_exists("{$this->prefix}{$key}", $_SESSION)) {
65
            $default = $_SESSION["{$this->prefix}{$key}"];
66 4
        }
67
        return $default;
68 4
    }
69 4
70 2
    /**
71 2
     * Set/Stores a provided values with a given key.
72 4
     *
73
     * @param string $key The key used to store the value in session.
74
     * @param mixed $value The value to store under the provided key.
75
     *
76
     * @return self|$this|SessionDriverInterface Self instance for
77
     *   method call chains.
78 2
     */
79
    public function set($key, $value)
80 2
    {
81 2
        $_SESSION["{$this->prefix}{$key}"] = $value;
82 2
        return $this;
83
    }
84
85
    /**
86
     * Erases the values stored with the given key.
87
     *
88 2
     * @param string $key The key used to store the value in session.
89
     *
90 2
     * @return self|$this|SessionDriverInterface Self instance for
91 2
     *   method call chains.
92 2
     */
93
    public function erase($key)
94
    {
95
        unset($_SESSION["{$this->prefix}{$key}"]);
96
        return $this;
97
    }
98
}
99