Completed
Push — master ( a03250...475c1c )
by Steevan
02:25
created

assertAllTranslationsFound()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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