GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 50cef7...02ca20 )
by Rolf
01:54
created

Session   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 16
c 10
b 0
f 0
lcom 1
cbo 3
dl 0
loc 102
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 16 5
A processGetState() 0 10 2
A insertState() 0 7 1
A updateState() 0 7 1
A isPersisted() 0 8 2
B getEntityIds() 0 16 5
1
<?php
2
namespace izzum\statemachine\persistence;
3
use izzum\statemachine\Identifier;
4
use izzum\statemachine\State;
5
6
/**
7
 * the Session persistence adapter uses php sessions for persistence.
8
 *
9
 * Keep in mind that:
10
 * - php sessions have a limited lifetime.
11
 * - php sessions itself can have a different backend adapter, configurable.
12
 * - php sessions are only valid for one user's session (unless a session id is
13
 * forced)
14
 *
15
 * A simple adapter for a proof of concept and an example.
16
 * it's possible to use this adapter for for instance gui wizards.
17
 * 
18
 * see also the /examples/session for how to use this adapter.
19
 *
20
 * TRICKY: make sure output is not already sent when instantiating this adapter.
21
 *
22
 * @author Rolf Vreijdenberger
23
 */
24
class Session extends Adapter {
25
    /**
26
     * the namespace of the session
27
     *
28
     * @var string
29
     */
30
    private $namespace;
31
32
    /**
33
     *
34
     * @param string $namespace
35
     *            optional, defaults to izzum
36
     * @param string $session_id
37
     *            optional force a session id, used for testing purposes
38
     */
39
    public function __construct($namespace = 'izzum', $session_id = null)
0 ignored issues
show
Coding Style introduced by
__construct 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...
40
    {
41
        if (session_status() === PHP_SESSION_NONE) {
42
            if ($session_id !== null) {
43
                session_id($session_id);
44
            }
45
            session_start();
46
        }
47
        $this->namespace = $namespace;
48
        if (!isset($_SESSION)) {
49
            $_SESSION = array();
50
        }
51
        if (!isset($_SESSION [$this->namespace])) {
52
            $_SESSION [$this->namespace] = array();
53
        }
54
    }
55
    
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function processGetState(Identifier $identifier)
0 ignored issues
show
Coding Style introduced by
processGetState 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...
61
    {
62
        $key = $identifier->getId();
63
        if (isset($_SESSION [$this->namespace] [$key])) {
64
            $state = $_SESSION [$this->namespace] [$key]->state;
65
        } else {
66
            $state = State::STATE_UNKNOWN;
67
        }
68
        return $state;
69
    }
70
71
    
72
    /**
73
     * {@inheritDoc}
74
     */
75
    protected function insertState(Identifier $identifier, $state, $message = null)
0 ignored issues
show
Coding Style introduced by
insertState 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...
76
    {
77
        // set object on the session
78
        $data = new StorageData($identifier, $state, $message);
79
        $key = $identifier->getId();
80
        $_SESSION [$this->namespace] [$key] = $data;
81
    }
82
    
83
    /**
84
     * {@inheritDoc}
85
     */
86
    protected function updateState(Identifier $identifier, $state, $message = null)
0 ignored issues
show
Coding Style introduced by
updateState 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...
87
    {
88
        // set object on the session
89
        $data = new StorageData($identifier, $state, $message);
90
        $key = $identifier->getId();
91
        $_SESSION [$this->namespace] [$key] = $data;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function isPersisted(Identifier $identifier)
0 ignored issues
show
Coding Style introduced by
isPersisted 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...
98
    {
99
        $key = $identifier->getId();
100
        if (!isset($_SESSION [$this->namespace] [$key])) {
101
            return false;
102
        }
103
        return true;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function getEntityIds($machine, $state = null)
0 ignored issues
show
Coding Style introduced by
getEntityIds 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...
110
    {
111
        $ids = array();
112
        foreach ($_SESSION [$this->namespace] as $key => $storage) {
113
            if (strstr($key, $machine)) {
114
                if ($state) {
115
                    if ($storage->state === $state) {
116
                        $ids [] = $storage->id;
117
                    }
118
                } else {
119
                    $ids [] = $storage->id;
120
                }
121
            }
122
        }
123
        return $ids;
124
    }
125
}
126