Completed
Push — master ( 7af5d4...df31c7 )
by Jared
06:47
created

SessionMiddleware::__invoke()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 64
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 8.6346
c 0
b 0
f 0
cc 5
eloc 40
nc 4
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
        // Check if sessions are disabled for the route
31
        $route = (array) array_value($this->app['routeInfo'], 1);
32
        $params = (array) array_value($route, 2);
33
        if (array_value($params, 'no_session')) {
34
            return $next($req, $res);
35
        }
36
37
        $lifetime = $config->get('sessions.lifetime');
38
        $hostname = $config->get('app.hostname');
39
        ini_set('session.use_trans_sid', false);
40
        ini_set('session.use_only_cookies', true);
41
        ini_set('url_rewriter.tags', '');
42
        ini_set('session.gc_maxlifetime', $lifetime);
43
44
        // set the session name
45
        $defaultSessionTitle = $config->get('app.title').'-'.$hostname;
46
        $sessionTitle = $config->get('sessions.name', $defaultSessionTitle);
47
        $safeSessionTitle = str_replace(['.', ' ', "'", '"'], ['', '_', '', ''], $sessionTitle);
48
        session_name($safeSessionTitle);
49
50
        // set the session cookie parameters
51
        session_set_cookie_params(
52
            $lifetime, // lifetime
53
            '/', // path
54
            '.'.$hostname, // domain
55
            $req->isSecure(), // secure
56
            true // http only
57
        );
58
59
        // register session_write_close as a shutdown function
60
        session_register_shutdown();
61
62
        // install any custom session handlers
63
        $class = $config->get('sessions.driver');
64
        if ($class) {
65
            $handler = new $class($this->app);
66
            $handler::registerHandler($handler);
67
        }
68
69
        session_start();
70
71
        // fix the session cookie
72
        Utility::setCookieFixDomain(
73
            session_name(),
74
            session_id(),
75
            time() + $lifetime,
76
            '/',
77
            $hostname,
78
            $req->isSecure(),
79
            true
80
        );
81
82
        // make the newly started session in our request
83
        $req->setSession($_SESSION);
84
85
        return $next($req, $res);
86
    }
87
}
88