Completed
Push — master ( 710c27...af4131 )
by Jared
02:05
created

SessionMiddleware::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 9.0544
c 0
b 0
f 0
cc 4
eloc 35
nc 3
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace Infuse\Middleware;
13
14
use Infuse\HasApp;
15
use Infuse\Request;
16
use Infuse\Response;
17
use Infuse\Utility;
18
19
class SessionMiddleware
20
{
21
    use HasApp;
22
23
    public function __invoke(Request $req, Response $res, callable $next)
0 ignored issues
show
Coding Style introduced by
__invoke 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
    {
25
        $config = $this->app['config'];
26
        if (!$config->get('sessions.enabled') || $req->isApi()) {
27
            return $next($req, $res);
28
        }
29
30
        $lifetime = $config->get('sessions.lifetime');
31
        $hostname = $config->get('app.hostname');
32
        ini_set('session.use_trans_sid', false);
33
        ini_set('session.use_only_cookies', true);
34
        ini_set('url_rewriter.tags', '');
35
        ini_set('session.gc_maxlifetime', $lifetime);
36
37
        // set the session name
38
        $sessionTitle = $config->get('app.title').'-'.$hostname;
39
        $safeSessionTitle = str_replace(['.', ' ', "'", '"'], ['', '_', '', ''], $sessionTitle);
40
        session_name($safeSessionTitle);
41
42
        // set the session cookie parameters
43
        session_set_cookie_params(
44
            $lifetime, // lifetime
45
            '/', // path
46
            '.'.$hostname, // domain
47
            $req->isSecure(), // secure
48
            true // http only
49
        );
50
51
        // register session_write_close as a shutdown function
52
        session_register_shutdown();
53
54
        // install any custom session handlers
55
        $class = $config->get('sessions.driver');
56
        if ($class) {
57
            $handler = new $class($this->app);
58
            $handler::registerHandler($handler);
59
        }
60
61
        session_start();
62
63
        // fix the session cookie
64
        Utility::setCookieFixDomain(
65
            session_name(),
66
            session_id(),
67
            time() + $lifetime,
68
            '/',
69
            $hostname,
70
            $req->isSecure(),
71
            true
72
        );
73
74
        // make the newly started session in our request
75
        $req->setSession($_SESSION);
76
77
        return $next($req, $res);
78
    }
79
}
80