JarvisSession   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 0
cbo 0
dl 0
loc 66
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 3 1
A setActivePlugin() 0 3 1
A getActivePlugin() 0 3 1
A sessionInProgress() 0 3 1
A terminate() 0 4 1
A set() 0 3 1
A get() 0 3 2
A reset() 0 3 1
1
<?php
2
3
namespace JarvisPHP\Core;
4
5
/**
6
 * Class for manage session (context)
7
 * @author Stefano Bianchini
8
 * @website http://www.stefanobianchini.net
9
 */
10
class JarvisSession {
11
    
12
    /**
13
     * Begin session
14
     */
15
    public static function start() {
16
        session_start();
17
    }
18
    
19
    /**
20
     * Set active plugin to session
21
     * @param string $pluginName
22
     */
23
    public static function setActivePlugin($pluginName) {
0 ignored issues
show
Coding Style introduced by
setActivePlugin 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...
24
        $_SESSION['active_plugin'] = $pluginName;
25
    }
26
    
27
    /**
28
     * Get active plugin
29
     * @return string
30
     */
31
    public static function getActivePlugin() {
0 ignored issues
show
Coding Style introduced by
getActivePlugin 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...
32
        return $_SESSION['active_plugin'];
33
    }
34
    
35
    /**
36
     * Check if a session is in progress
37
     * @return boolena
38
     */
39
    public static function sessionInProgress() {
0 ignored issues
show
Coding Style introduced by
sessionInProgress 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
        return !empty($_SESSION['active_plugin']);
41
    }
42
    
43
    /**
44
     * Ends the session
45
     */
46
    public static function terminate() {
47
        session_unset('active_plugin');
48
        session_destroy();
49
    }
50
    
51
    /**
52
     * Set a variable to session
53
     * @param string $name
54
     * @param string $value
55
     */
56
    public static function set($name, $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...
57
        $_SESSION[$name] = $value;
58
    }
59
    
60
    /**
61
     * Get a variable from session
62
     * @param string $name
63
     * @return string
64
     */
65
    public static function get($name) {
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...
66
        return isset($_SESSION[$name]) ? $_SESSION[$name] : false;
67
    }
68
    
69
    /**
70
     * Reset session
71
     */
72
    public static function reset() {
73
        session_unset();
74
    }
75
}