Lock::lock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
* Vperyod Simple Lock
4
*
5
* PHP version 5
6
*
7
* Copyright (C) 2016 Jake Johns
8
*
9
* This software may be modified and distributed under the terms
10
* of the MIT license.  See the LICENSE file for details.
11
*
12
* @category  Session
13
* @package   Vperyod\SimpleLock
14
* @author    Jake Johns <[email protected]>
15
* @copyright 2016 Jake Johns
16
* @license   http://jnj.mit-license.org/2016 MIT License
17
* @link      http://jakejohns.net
18
 */
19
20
namespace Vperyod\SimpleLock;
21
22
use Psr\Http\Message\ServerRequestInterface as Request;
23
24
/**
25
 * Lock
26
 *
27
 * @category Session
28
 * @package  Vperyod\SimpleLock
29
 * @author   Jake Johns <[email protected]>
30
 * @license  http://jnj.mit-license.org/ MIT License
31
 * @link     https://github.com/vperyod/vperyod.simple-lock
32
 */
33
class Lock
34
{
35
    /**
36
     * Cookie
37
     *
38
     * @var array
39
     *
40
     * @access protected
41
     */
42
    protected $cookie;
43
44
    /**
45
     * Session key for lock status
46
     *
47
     * @var string
48
     *
49
     * @access protected
50
     */
51
    protected $key = 'vperyod/simplelock:unlocked';
52
53
    /**
54
     * Session key for intent
55
     *
56
     * @var string
57
     *
58
     * @access protected
59
     */
60
    protected $intent = 'vperyod/simplelock:intent';
61
62
    /**
63
     * Create a session lock
64
     *
65
     * @access public
66
     */
67 2
    public function __construct()
68
    {
69 2
        $this->session();
70 2
    }
71
72
    /**
73
     * Unlock a session
74
     *
75
     * @return void
76
     *
77
     * @access public
78
     */
79 1
    public function unlock()
0 ignored issues
show
Coding Style introduced by
unlock 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...
80
    {
81 1
        $_SESSION[$this->key] = true;
82 1
    }
83
84
    /**
85
     * Lock a session
86
     *
87
     * @return void
88
     *
89
     * @access public
90
     */
91 1
    public function lock()
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
Coding Style introduced by
lock 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...
92
    {
93 1
        $_SESSION[$this->key] = false;
94 1
    }
95
96
    /**
97
     * Set intent
98
     *
99
     * @param Request $request intended request
100
     *
101
     * @return void
102
     *
103
     * @access public
104
     */
105 1
    public function setIntent(Request $request)
0 ignored issues
show
Coding Style introduced by
setIntent 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...
106
    {
107 1
        $_SESSION[$this->intent] = $request;
108 1
    }
109
110
    /**
111
     * Get and clear intent
112
     *
113
     * @return null | Request
114
     *
115
     * @access public
116
     */
117 1
    public function getIntent()
0 ignored issues
show
Coding Style introduced by
getIntent 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...
118
    {
119 1
        if (isset($_SESSION[$this->intent])) {
120 1
            $intent = $_SESSION[$this->intent];
121 1
            unset($_SESSION[$this->intent]);
122 1
            return $intent;
123
        }
124 1
        return null;
125
    }
126
127
    /**
128
     * Is session unlocked?
129
     *
130
     * @return bool
131
     *
132
     * @access public
133
     */
134 1
    public function isUnlocked()
0 ignored issues
show
Coding Style introduced by
isUnlocked 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...
135
    {
136 1
        return isset($_SESSION[$this->key])
137 1
            && true === $_SESSION[$this->key];
138
    }
139
140
    /**
141
     * Resume session
142
     *
143
     * @return bool
144
     *
145
     * @access protected
146
     */
147 2
    protected function session()
148
    {
149 2
        return (session_id() !== '') ?: session_start();
150
    }
151
}
152