Passed
Pull Request — master (#82)
by Dominik
32:54
created

SessionPersistence::initializeSessionFromRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace PSR7Sessions\Storageless\Session\Zend;
22
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use PSR7Sessions\Storageless\Http\SessionMiddleware;
26
use PSR7Sessions\Storageless\Session\SessionInterface;
27
use UnexpectedValueException;
28
use Zend\Expressive\Session\SessionInterface as ZendSessionInterface;
29
use Zend\Expressive\Session\SessionPersistenceInterface as ZendSessionPersistenceInterface;
30
use function sprintf;
31
32
final class SessionPersistence implements ZendSessionPersistenceInterface
33
{
34
    public function initializeSessionFromRequest(ServerRequestInterface $request) : ZendSessionInterface
35
    {
36
        /** @var SessionInterface|null $storagelessSession */
37
        $storagelessSession = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
38
        if (! $storagelessSession instanceof SessionInterface) {
39
            throw new UnexpectedValueException(
40
                sprintf(
41
                    'Please add this following middleware "%s" before execute this method "%s"',
42
                    SessionMiddleware::class,
43
                    __METHOD__
44
                )
45
            );
46
        }
47
48
        return new SessionAdapter($storagelessSession);
49
    }
50
51
    public function persistSession(ZendSessionInterface $session, ResponseInterface $response) : ResponseInterface
52
    {
53
        return $response;
54
    }
55
}
56