Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SymfonyLocaleContextBuilder
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2namespace Apie\ApieBundle\ContextBuilders;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\ContextBuilders\ContextBuilderInterface;
6use Apie\Core\ContextConstants;
7use Symfony\Component\Translation\LocaleSwitcher;
8
9final class SymfonyLocaleContextBuilder implements ContextBuilderInterface
10{
11    public function __construct(private readonly ?LocaleSwitcher $localeSwitcher = null)
12    {
13    }
14
15    public function process(ApieContext $context): ApieContext
16    {
17        $locale = $this->localeSwitcher?->getLocale();
18        if (!$locale) {
19            return $context;
20        }
21
22        if (!$context->hasContext(ContextConstants::LOCALE)) {
23            $context = $context->withContext(ContextConstants::LOCALE, $locale);
24        }
25        if (!$context->hasContext(ContextConstants::ACCEPT_LOCALE)) {
26            $context = $context->withContext(ContextConstants::ACCEPT_LOCALE, $locale);
27        }
28        if (!$context->hasContext(ContextConstants::DATA_LOCALE)) {
29            $context = $context->withContext(ContextConstants::DATA_LOCALE, $locale);
30        }
31
32        return $context;
33    }
34}