| Conditions | 5 |
| Paths | 4 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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 |
||
| 23 | public function __invoke(Request $req, Response $res, callable $next) |
||
|
|
|||
| 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 |
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: