Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.33% |
11 / 15 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
SessionContextBuilder | |
73.33% |
11 / 15 |
|
0.00% |
0 / 1 |
6.68 | |
0.00% |
0 / 1 |
process | |
73.33% |
11 / 15 |
|
0.00% |
0 / 1 |
6.68 |
1 | <?php |
2 | namespace Apie\LaravelApie\ContextBuilders; |
3 | |
4 | use Apie\Core\Context\ApieContext; |
5 | use Apie\Core\ContextBuilders\ContextBuilderInterface; |
6 | use Apie\Core\ContextConstants; |
7 | use Apie\Core\Enums\RequestMethod; |
8 | use Apie\Core\ValueObjects\Utils; |
9 | use Illuminate\Http\Request; |
10 | use Illuminate\Session\SymfonySessionDecorator; |
11 | use Symfony\Component\HttpFoundation\Session\Session; |
12 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
13 | |
14 | class 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 | } |