Passed
Pull Request — master (#82)
by Dominik
31:06
created

SessionPersistence::persistSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 Zend\Expressive\Session\SessionInterface as ZendSessionInterface;
28
use Zend\Expressive\Session\SessionPersistenceInterface as ZendSessionPersistenceInterface;
29
use function sprintf;
30
31
final class SessionPersistence implements ZendSessionPersistenceInterface
32
{
33
    public function initializeSessionFromRequest(ServerRequestInterface $request) : ZendSessionInterface
34
    {
35
        /** @var SessionInterface|null $storagelessSession */
36
        $storagelessSession = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
37
        if ($storagelessSession === null) {
38
            throw new \UnexpectedValueException(
39
                sprintf(
40
                    'Please add the following middleware "%s" before execute this method "%s"',
41
                    SessionMiddleware::class,
42
                    __METHOD__
43
                )
44
            );
45
        }
46
47
        return new SessionAdapter($storagelessSession);
48
    }
49
50
    public function persistSession(ZendSessionInterface $session, ResponseInterface $response) : ResponseInterface
51
    {
52
        return $response;
53
    }
54
}
55