Driver::erase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/session package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Session\Driver;
11
12
use Slick\Session\SessionDriverInterface;
13
14
/**
15
 * Session driver, base class for all session drivers
16
 *
17
 * @package Slick\Session\Driver
18
 * @author  Filipe Silva <[email protected]>
19
 */
20
abstract class Driver implements SessionDriverInterface
21
{
22
23
    /**
24
     * @var string Session prefix on key names
25
     */
26
    protected $prefix;
27
28
    /**
29
     * Driver constructor.
30
     * 
31
     * The $options parameter is an associative array where key will be matched
32
     * with the driver property and if property exists the correspondent value
33
     * will be assigned.
34
     * 
35
     * @param array $options
36
     */
37 14
    public function __construct(array $options = [])
38
    {
39 14
        foreach ($options as $name => $option) {
40 4
            if (property_exists($this, $name)) {
41 4
                $this->{$name} = $option;
42 4
            }
43 14
        }
44 14
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function set($key, $value)
0 ignored issues
show
Coding Style introduced by
set uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
50
    {
51 2
        $prefix = $this->prefix;
52 2
        $_SESSION[$prefix.$key] = $value;
53 2
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 4
    public function get($key, $default = null)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
60
    {
61 4
        $key = $this->prefix.$key;
62 4
        $value = $default;
63 4
        if (array_key_exists($key, $_SESSION)) {
64 2
            $value = $_SESSION[$key];
65 2
        }
66 4
        return $value;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 2
    public function erase($key)
0 ignored issues
show
Coding Style introduced by
erase uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
73
    {
74 2
        $prefix = $this->prefix;
75 2
        unset($_SESSION[$prefix.$key]);
76 2
        return $this;
77
    }
78
79
    /**
80
     * Sets the prefix for session key names
81
     * 
82
     * @param string $prefix
83
     * 
84
     * @return $this
85
     */
86 2
    public function setPrefix($prefix)
87
    {
88 2
        $this->prefix = $prefix;
89 2
        return $this;
90
    }
91
}