Conditions | 5 |
Paths | 4 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public function __invoke(Request $req, Response $res, callable $next) |
||
23 | { |
||
24 | $config = $this->app['config']; |
||
25 | if (!$config->get('sessions.enabled') || $req->isApi()) { |
||
26 | return $next($req, $res); |
||
27 | } |
||
28 | |||
29 | // Check if sessions are disabled for the route |
||
30 | $route = (array) array_value($this->app['routeInfo'], 1); |
||
31 | $params = (array) array_value($route, 2); |
||
32 | if (array_value($params, 'no_session')) { |
||
33 | return $next($req, $res); |
||
34 | } |
||
35 | |||
36 | $lifetime = $config->get('sessions.lifetime'); |
||
37 | $hostname = $config->get('app.hostname'); |
||
38 | ini_set('session.use_trans_sid', false); |
||
39 | ini_set('session.use_only_cookies', true); |
||
40 | ini_set('url_rewriter.tags', ''); |
||
41 | ini_set('session.gc_maxlifetime', $lifetime); |
||
42 | |||
43 | // set the session name |
||
44 | $defaultSessionTitle = $config->get('app.title').'-'.$hostname; |
||
45 | $sessionTitle = $config->get('sessions.name', $defaultSessionTitle); |
||
46 | $safeSessionTitle = str_replace(['.', ' ', "'", '"'], ['', '_', '', ''], $sessionTitle); |
||
47 | session_name($safeSessionTitle); |
||
48 | |||
49 | // set the session cookie parameters |
||
50 | session_set_cookie_params( |
||
51 | $lifetime, // lifetime |
||
52 | '/', // path |
||
53 | '.'.$hostname, // domain |
||
54 | $req->isSecure(), // secure |
||
55 | true // http only |
||
56 | ); |
||
57 | |||
58 | // register session_write_close as a shutdown function |
||
59 | session_register_shutdown(); |
||
60 | |||
61 | // install any custom session handlers |
||
62 | $class = $config->get('sessions.driver'); |
||
63 | if ($class) { |
||
64 | $handler = new $class($this->app); |
||
65 | $handler::registerHandler($handler); |
||
66 | } |
||
67 | |||
68 | session_start(); |
||
69 | |||
70 | // make the newly started session in our request |
||
71 | $req->setSession($_SESSION); |
||
72 | |||
73 | return $next($req, $res); |
||
74 | } |
||
75 | } |
||
76 |