Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionContextBuilder
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
6.68
0.00% covered (danger)
0.00%
0 / 1
 process
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
6.68
1<?php
2namespace Apie\LaravelApie\ContextBuilders;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\ContextBuilders\ContextBuilderInterface;
6use Apie\Core\ContextConstants;
7use Apie\Core\Enums\RequestMethod;
8use Apie\Core\ValueObjects\Utils;
9use Illuminate\Http\Request;
10use Illuminate\Session\SymfonySessionDecorator;
11use Symfony\Component\HttpFoundation\Session\Session;
12use Symfony\Component\HttpFoundation\Session\SessionInterface;
13
14class SessionContextBuilder implements ContextBuilderInterface
15{
16    public function process(ApieContext $context): ApieContext
17    {
18        $request = request();
19        // @phpstan-ignore instanceof.alwaysTrue
20        if ($request instanceof Request && $request->hasSession()) {
21            $session =  $request->session();
22            $context = $context->withContext(SessionInterface::class, new SymfonySessionDecorator($session));
23            // TODO: move to its own context builder
24            if ($context->getContext(RequestMethod::class, false) === RequestMethod::GET) {
25                if ($session->has('_filled_in') && $context->hasContext(ContextConstants::DISPLAY_FORM)) {
26                    $context = $context->withContext(
27                        ContextConstants::RAW_CONTENTS,
28                        Utils::toArray($session->get('_filled_in', []))
29                    );
30                }
31                $context = $context->withContext(
32                    ContextConstants::VALIDATION_ERRORS,
33                    Utils::toArray($session->get('_validation_errors', []))
34                );
35                // TODO: unset from session
36            }
37        }
38
39        return $context;
40    }
41}