Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SessionContextBuilder | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\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 Symfony\Component\HttpFoundation\RequestStack; |
| 10 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 11 | |
| 12 | class SessionContextBuilder implements ContextBuilderInterface |
| 13 | { |
| 14 | public function __construct(private readonly RequestStack $requestStack) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | public function process(ApieContext $context): ApieContext |
| 19 | { |
| 20 | $request = $this->requestStack->getMainRequest(); |
| 21 | if ($request && $request->hasSession()) { |
| 22 | $session = $request->getSession(); |
| 23 | $context = $context->withContext(SessionInterface::class, $session); |
| 24 | // TODO: move to its own context builder |
| 25 | if ($context->getContext(RequestMethod::class, false) === RequestMethod::GET) { |
| 26 | $filledIn = $session->get('_filled_in', []); |
| 27 | if ($context->hasContext(ContextConstants::DISPLAY_FORM)) { |
| 28 | $context = $context->withContext( |
| 29 | ContextConstants::RAW_CONTENTS, |
| 30 | Utils::toArray($filledIn) |
| 31 | ); |
| 32 | } |
| 33 | $context = $context->withContext( |
| 34 | ContextConstants::VALIDATION_ERRORS, |
| 35 | Utils::toArray($session->get('_validation_errors', [])) |
| 36 | ); |
| 37 | // TODO: unset from session |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return $context; |
| 42 | } |
| 43 | } |