Completed
Push — master ( f1d123...cb630f )
by jake
03:26 queued 02:22
created

SessionAwareTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSession() 0 12 2
A getSessionSegment() 0 7 1
1
<?php
2
/**
3
 * Session Handler
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Trait
13
 * @package   Vperyod\SessionHandler
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/vperyod/vperyod.session-handler
18
 */
19
20
namespace Vperyod\SessionHandler;
21
22
use Aura\Session;
23
use Psr\Http\Message\ResponseInterface as Response;
24
use Psr\Http\Message\ServerRequestInterface as Request;
25
26
/**
27
 * Session aware trait
28
 *
29
 * Trait for objects which need to know where the session attribute is stored in
30
 * the request and some helpers to interact with it.
31
 *
32
 * @category Trait
33
 * @package  Vperyod\SessionHandler
34
 * @author   Jake Johns <[email protected]>
35
 * @license  http://jnj.mit-license.org/2016 MIT License
36
 * @link     https://github.com/vperyod/vperyod.session-handler
37
 */
38
trait SessionAwareTrait
39
{
40
    /**
41
     * Get session from request
42
     *
43
     * @param Request $request PSR7 Request
44
     *
45
     * @return Session\Session
46
     * @throws Exception if session attribute is invalid
47
     *
48
     * @access protected
49
     */
50
    protected function getSession(Request $request) : Session\Session
51
    {
52
        $session = $request->getAttribute(SessionHandler::SESSION_ATTRIBUTE);
53
54
        if (! $session instanceof Session\Session) {
0 ignored issues
show
Bug introduced by
The class Aura\Session\Session does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
55
            throw new \Exception(
56
                'Session not available in request at: '
57
                . SessionHandler::SESSION_ATTRIBUTE
58
            );
59
        }
60
        return $session;
61
    }
62
63
    /**
64
     * GetSessionSegment
65
     *
66
     * @param Request $request incoming request
67
     * @param string  $name    segment name
68
     *
69
     * @return Session\SegmentInterface
70
     *
71
     * @access protected
72
     */
73
    protected function getSessionSegment(
74
        Request $request,
75
        string $name
76
    ) : Session\SegmentInterface {
77
        $session = $this->getSession($request);
78
        return $session->getSegment($name);
79
    }
80
}
81