TranslationsNotFoundListener::setAllowFallbacks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace steevanb\DevBundle\EventListener;
6
7
use steevanb\DevBundle\Exception\TranslationsNotFoundException;
8
use Symfony\Component\Translation\DataCollectorTranslator;
9
10
class TranslationsNotFoundListener
11
{
12
    /** @var DataCollectorTranslator */
13
    protected $dataCollectorTranslator;
14
15
    /** @var bool */
16
    protected $allowFallbacks = false;
17
18
    public function __construct(DataCollectorTranslator $dataCollectorTranslator)
19
    {
20
        $this->dataCollectorTranslator = $dataCollectorTranslator;
21
    }
22
23
    public function setAllowFallbacks(bool $allow): self
24
    {
25
        $this->allowFallbacks = $allow;
26
27
        return $this;
28
    }
29
30
    public function assertAllTranslationsFound(): self
31
    {
32
        $missings = array();
33
        foreach ($this->dataCollectorTranslator->getCollectedMessages() as $message) {
34
            if ($message['state'] === DataCollectorTranslator::MESSAGE_MISSING) {
35
                $missings[] = $message;
36
            } elseif (
37
                $message['state'] === DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK
38
                && $this->allowFallbacks === false
39
            ) {
40
                $message['locale'] = 'fallback ' . $message['locale'] . ' found ';
41
                $message['locale'] .= 'but not allowed by config dev.translation.allow_fallbacks)';
42
                $missings[] = $message;
43
            }
44
        }
45
46
        if (count($missings) > 0) {
47
            throw new TranslationsNotFoundException($missings);
48
        }
49
50
        return $this;
51
    }
52
}
53