Completed
Push — master ( d8083a...61164d )
by Arnaud
13s queued 11s
created

configureTranslation()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.4689

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 33
ccs 13
cts 17
cp 0.7647
crap 6.4689
rs 9.0111
1
<?php
2
3
namespace LAG\AdminBundle\Configuration\Behavior;
4
5
use LAG\AdminBundle\Utils\TranslationUtils;
6
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
trait TranslationConfigurationTrait
11
{
12
    abstract public function get($name);
13
14 4
    protected function configureTranslation(OptionsResolver $resolver, string $pattern = 'lag.{admin}.{key}', string $catalog = 'messages')
15
    {
16
        $resolver
17 4
            ->setDefaults([
18
                'translation' => function (OptionsResolver $subResolver) use ($pattern, $catalog) {
19
                    $subResolver
20 4
                        ->setDefaults([
21 4
                            'enabled' => true,
22 4
                            'pattern' => $pattern,
23 4
                            'catalog' => $catalog,
24
                        ])
25
                    ;
26 4
                },
27
            ])
28 4
            ->setAllowedTypes('translation', 'array')
29
            ->setNormalizer('translation', function (Options $options, $value) {
30 4
                if (!array_key_exists('enabled', $value)) {
31
                    throw new InvalidOptionsException('Admin translation enabled parameter should be defined');
32
                }
33
34 4
                if (!is_bool($value['enabled'])) {
35
                    throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean');
36
                }
37
38 4
                if (!array_key_exists('pattern', $value)) {
39
                    $value['pattern'] = '{admin}.{key}';
40
                }
41
42 4
                if ($value['enabled'] && false === strstr($value['pattern'], '{key}')) {
43
                    throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder, given "'.$value['pattern'].'"');
44
                }
45
46 4
                return $value;
47 4
            })
48
        ;
49 4
    }
50
51
    public function isTranslationEnabled(): bool
52
    {
53
        return true === $this->get('translation')['enabled'];
54
    }
55
56
    public function getTranslationPattern(): string
57
    {
58
        return $this->get('translation')['pattern'];
59
    }
60
61
    public function getTranslationCatalog(): string
62
    {
63
        return $this->get('translation')['catalog'];
64
    }
65
66
    public function getTranslationKey(string $text, $adminName = null): string
67
    {
68
        $text = ucfirst($text);
69
70
        if (!$this->isTranslationEnabled()) {
71
            $text = TranslationUtils::getTranslationKey(
72
                $this->getTranslationPattern(),
73
                $adminName,
74
                'create'
75
            );
76
        }
77
78
        return $text;
79
    }
80
}
81