GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 650ed2...bb2ad6 )
by jake
01:53
created

SessionHandler::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 5
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 * Molniya - Flash Messenger
4
 *
5
 * PHP version 5
6
 *
7
 * This program is free software: you can redistribute it and/or modify it
8
 * under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or (at your
10
 * option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @category  Middleware
21
 * @package   Jnjxp\Molniya
22
 * @author    Jake Johns <[email protected]>
23
 * @copyright 2016 Jake Johns
24
 * @license   http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3
25
 * @link      https://github.com/jnjxp/jnjxp.molniya
26
 */
27
28
namespace Jnjxp\Molniya;
29
30
use Aura\Session\SessionFactory;
31
use Jnjxp\Molniya\FlashMessengerFactory as MessengerFactory;
32
33
use Psr\Http\Message\ServerRequestInterface as Request;
34
use Psr\Http\Message\ResponseInterface as Response;
35
36
/**
37
 * SessionHandler
38
 *
39
 * @category Middleware
40
 * @package  Jnjxp\Molniya
41
 * @author   Jake Johns <[email protected]>
42
 * @license  http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3
43
 * @link     https://github.com/jnjxp/jnjxp.molniya
44
 */
45
class SessionHandler
46
{
47
    /**
48
     * Session factory
49
     *
50
     * @var SessionFactory
51
     *
52
     * @access protected
53
     */
54
    protected $sessionFactory;
55
56
    /**
57
     * Flash messenger factory
58
     *
59
     * @var MessengerFactory
60
     *
61
     * @access protected
62
     */
63
    protected $messengerFactory;
64
65
    /**
66
     * Session attribute
67
     *
68
     * @var string
69
     *
70
     * @access protected
71
     */
72
    protected $sessionAttribute = 'aura/session:session';
73
74
    /**
75
     * Messenger attribute
76
     *
77
     * @var string
78
     *
79
     * @access protected
80
     */
81
    protected $messengerAttribute = 'jnjxp/molniya:messenger';
82
83
    /**
84
     * Messenger namespace
85
     *
86
     * @var string
87
     *
88
     * @access protected
89
     */
90
    protected $messengerNamespace = 'Jnjxp\Molniya';
91
92
    /**
93
     * Create a session handler
94
     *
95
     * @param SessionFactory   $sessionFactory   Session factory
96
     * @param MessengerFactory $messengerFactory Flash messenger factory
97
     *
98
     * @access public
99
     */
100 1
    public function __construct(
101
        SessionFactory $sessionFactory = null,
102
        MessengerFactory $messengerFactory = null
103
    ) {
104 1
        $this->sessionFactory = $sessionFactory ?: new SessionFactory();
105 1
        $this->messengerFactory = $messengerFactory ?: new MessengerFactory();
106 1
    }
107
108
    /**
109
     * Set session attribute
110
     *
111
     * @param string $attr name of attribute for session
112
     *
113
     * @return $this
114
     *
115
     * @access public
116
     */
117 1
    public function setSessionAttribute($attr)
118
    {
119 1
        $this->sessionAttribute = $attr;
120 1
        return $this;
121
    }
122
123
    /**
124
     * Set messenger attribute
125
     *
126
     * @param string $attr name of attribute for messenger
127
     *
128
     * @return $this
129
     *
130
     * @access public
131
     */
132 1
    public function setMessengerAttribute($attr)
133
    {
134 1
        $this->messengerAttribute = $attr;
135 1
        return $this;
136
    }
137
138
    /**
139
     * Set messenger namespace
140
     *
141
     * @param string $name namespace for messenger segment
142
     *
143
     * @return $this
144
     *
145
     * @access public
146
     */
147 1
    public function setMessengerNamespace($name)
148
    {
149 1
        $this->messengerNamespace = $name;
150 1
        return $this;
151
    }
152
153
    /**
154
     * Create session and messenger and set them as request attributes
155
     *
156
     * @param Request  $request  PSR7 Request
157
     * @param Response $response PSR7 Response
158
     * @param callable $next     Next callable middleware
159
     *
160
     * @return Response
161
     *
162
     * @access public
163
     */
164 1
    public function __invoke(Request $request, Response $response, callable $next)
165
    {
166
        // Create Session
167 1
        $session = $this->sessionFactory
168 1
            ->newInstance($request->getCookieParams());
169
170
        // Set session on request if attribute present
171 1
        if ($this->sessionAttribute !== null) {
172 1
            $request = $request->withAttribute(
173 1
                $this->sessionAttribute,
174
                $session
175 1
            );
176 1
        }
177
178
        // Create Messenger with Session
179 1
        $messenger = $this->messengerFactory
180 1
            ->setSession($session)
181 1
            ->newInstance($this->messengerNamespace);
182
183
        // Set messenger on request if attribute present
184 1
        if ($this->messengerAttribute !== null) {
185 1
            $request = $request->withAttribute(
186 1
                $this->messengerAttribute,
187
                $messenger
188 1
            );
189 1
        }
190
191 1
        return $next($request, $response);
192
    }
193
}
194