Completed
Push — master ( eff571...d844b5 )
by Joao
29s
created

SessionCacheEngine::checkSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace ByJG\Cache\Engine;
4
5
use ByJG\Cache\CacheEngineInterface;
6
7
class SessionCacheEngine implements CacheEngineInterface
8
{
9
10
    protected $prefix = null;
11
12
    /**
13
     * SessionCacheEngine constructor.
14
     *
15
     * @param string $prefix
16
     */
17
    public function __construct($prefix = 'cache')
18
    {
19
        $this->prefix = $prefix;
20
    }
21
22
23
    protected function checkSession()
24
    {
25
        if (session_status() == PHP_SESSION_NONE) {
26
            session_start();
27
        }
28
    }
29
30
    protected function keyName($key)
31
    {
32
        return $this->prefix . '-' . $key;
33
    }
34
35
    public function append($key, $str)
36
    {
37
        $this->checkSession();
38
39
        $keyName = $this->keyName($key);
40
41
        $current = $this->get($keyName);
42
        if ($current === false) {
43
            $this->set($keyName, $str);
0 ignored issues
show
Documentation introduced by
$str is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        } else {
45
            $this->set($keyName, $current . $str);
0 ignored issues
show
Documentation introduced by
$current . $str is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
        }
47
    }
48
49 View Code Duplication
    public function get($key, $ttl = 0)
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...
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...
50
    {
51
        $this->checkSession();
52
53
        $keyName = $this->keyName($key);
54
55
        if (isset($_SESSION[$keyName])) {
56
            return $_SESSION[$keyName];
57
        } else {
58
            return null;
59
        }
60
    }
61
62
    public function lock($key)
63
    {
64
        $this->checkSession();
65
66
        // Nothing to implement here;
67
    }
68
69 View Code Duplication
    public function release($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...
Coding Style introduced by
release 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...
70
    {
71
        $this->checkSession();
72
73
        $keyName = $this->keyName($key);
74
75
        if (isset($_SESSION[$keyName])) {
76
            unset($_SESSION[$keyName]);
77
        }
78
    }
79
80
    public function set($key, $object, $ttl = 0)
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...
81
    {
82
        $this->checkSession();
83
84
        $keyName = $this->keyName($key);
85
        $_SESSION[$keyName] = $object;
86
    }
87
88
    public function unlock($key)
89
    {
90
        $this->checkSession();
91
92
        // Nothing to implement here;
93
    }
94
95
    public function isAvailable()
96
    {
97
        try {
98
            $this->checkSession();
99
            return true;
100
        } catch (\Exception $ex) {
101
            return false;
102
        }
103
    }
104
}
105