SessionManager::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Session;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * Session Manager.
18
 */
19
class SessionManager extends Base
20
{
21
    /**
22
     * Event names.
23
     *
24
     * @var string
25
     */
26
    const EVENT_DESTROY = 'session.destroy';
27
28
    /**
29
     * Return true if the session is open.
30
     *
31
     * @static
32
     *
33
     * @return bool
34
     */
35
    public static function isOpen()
36
    {
37
        return session_id() !== '';
38
    }
39
40
    /**
41
     * Create a new session.
42
     */
43
    public function open()
0 ignored issues
show
Coding Style introduced by
open 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...
44
    {
45
        $this->configure();
46
47
        if (ini_get('session.auto_start') == 1) {
48
            session_destroy();
49
        }
50
51
        session_name('JM_SID');
52
        session_start();
53
54
        $this->sessionStorage->setStorage($_SESSION);
0 ignored issues
show
Documentation introduced by
The property sessionStorage does not exist on object<Jitamin\Foundation\Session\SessionManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
    }
56
57
    /**
58
     * Destroy the session.
59
     */
60
    public function close()
61
    {
62
        $this->dispatcher->dispatch(self::EVENT_DESTROY);
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Jitamin\Foundation\Session\SessionManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
64
        // Destroy the session cookie
65
        $params = session_get_cookie_params();
66
67
        setcookie(
68
            session_name(),
69
            '',
70
            time() - 42000,
71
            $params['path'],
72
            $params['domain'],
73
            $params['secure'],
74
            $params['httponly']
75
        );
76
77
        session_unset();
78
        session_destroy();
79
    }
80
81
    /**
82
     * Define session settings.
83
     */
84
    private function configure()
85
    {
86
        // Session cookie: HttpOnly and secure flags
87
        session_set_cookie_params(
88
            SESSION_DURATION,
89
            $this->helper->url->dir() ?: '/',
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Foundation\Session\SessionManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
            null,
91
            $this->request->isHTTPS(),
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Session\SessionManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
            true
93
        );
94
95
        // Avoid session id in the URL
96
        ini_set('session.use_only_cookies', '1');
97
        ini_set('session.use_trans_sid', '0');
98
99
        // Enable strict mode
100
        ini_set('session.use_strict_mode', '1');
101
102
        // Better session hash
103
        ini_set('session.hash_function', '1'); // 'sha512' is not compatible with FreeBSD, only MD5 '0' and SHA-1 '1' seems to work
104
        ini_set('session.hash_bits_per_character', 6);
105
106
        // Set an additional entropy
107
        ini_set('session.entropy_file', '/dev/urandom');
108
        ini_set('session.entropy_length', '256');
109
    }
110
}
111