Session   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 86.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 73
ccs 25
cts 29
cp 0.8621
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A close() 0 4 1
A setUser() 0 4 1
A getUser() 0 4 1
A set() 0 4 1
A has() 0 4 1
A get() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the "Kata 1" package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 */
13
14
namespace Component\Http;
15
16
use App\Model\User;
17
18
/**
19
 * Session.
20
 */
21
class Session
22
{
23
    /**
24
     * @param array $options
25
     * @param int   $timeout
26
     */
27 15
    public function __construct($options, $timeout = 3600)
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...
28
    {
29 15
        if (php_sapi_name() != 'cli') {
30
            session_cache_limiter('');
31
            ini_set('session.use_cookies', 1);
32
            session_start($options);
33
        }
34 15
        $lastTime = $this->get('app.last.time', false);
35 15
        if ($lastTime + $timeout < time()) {
36 5
            $_SESSION = [];
37 5
        }
38 15
        $this->set('app.last.time', time());
39 15
    }
40
41 4
    public function close()
0 ignored issues
show
Coding Style introduced by
close 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...
42
    {
43 4
        $_SESSION = [];
44 4
    }
45
46
    /**
47
     * @param User $user
48
     */
49 4
    public function setUser(User $user)
50
    {
51 4
        $this->set('app.user', $user);
52 4
    }
53
54
    /**
55
     * @return User|false
56
     */
57 15
    public function getUser()
58
    {
59 15
        return $this->get('app.user', null);
60
    }
61
62
    /**
63
     * @param string $key
64
     * @param mixed  $value
65
     */
66 15
    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...
67
    {
68 15
        $_SESSION[$key] = $value;
69 15
    }
70
71
    /**
72
     * @param string $key
73
     *
74
     * @return bool
75
     */
76 15
    public function has($key)
0 ignored issues
show
Coding Style introduced by
has 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...
77
    {
78 15
        return isset($_SESSION[$key]);
79
    }
80
81
    /**
82
     * @param string $key
83
     * @param mixed  $default
84
     */
85 15
    public function get($key, $default = false)
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...
86
    {
87 15
        if (!$this->has($key)) {
88 15
            return $default;
89
        }
90
91 13
        return $_SESSION[$key];
92
    }
93
}
94