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

TranslationsNotFoundListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setAllowFallbacks() 0 6 1
B assertAllTranslationsFound() 0 20 6
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