Completed
Push — remote-vetting ( e29980...33b61b )
by
unknown
02:08
created

RemoteVetting::getTranslation()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 2
nop 3
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Twig\Extensions\Extension;
20
21
use Symfony\Component\Translation\TranslatorInterface;
22
use Twig\Extension\AbstractExtension;
23
use Twig\TwigFilter;
24
25
final class RemoteVetting extends AbstractExtension
26
{
27
    private const TRANSLATION_TYPE_SCHACHOME = 'schachome';
28
    private const TRANSLATION_TYPE_ATTRIBUTES = 'attributes';
29
30
    /**
31
     * @var string
32
     */
33
    private $translationFile;
34
    /**
35
     * @var array|null
36
     */
37
    private $translations = null;
38
39
    public function __construct(string $translationFile)
40
    {
41
        $this->translationFile = $translationFile;
42
    }
43
44
    public function getFilters()
45
    {
46
        return [
47
            new TwigFilter('trans_rv_attribute', [$this, 'translateAttributeKey']),
48
            new TwigFilter('trans_rv_schachome', [$this, 'translateSchachome']),
49
        ];
50
    }
51
52
    public function translateAttributeKey($attributeName, $locale)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
    {
54
        return $this->getTranslation(self::TRANSLATION_TYPE_ATTRIBUTES, $locale, $attributeName);
55
    }
56
57
58
    public function translateSchachome($schachome, $locale)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        return $this->getTranslation(self::TRANSLATION_TYPE_SCHACHOME, $locale, $schachome);
61
    }
62
63
    private function getTranslation(string $type, string $locale, string $key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
64
    {
65
        $this->loadTranslationCache();
66
67
        if (!array_key_exists($type, $this->translations) ||
68
            !array_key_exists($key, $this->translations[$type]) ||
69
            !array_key_exists($locale, $this->translations[$type][$key])
70
        ) {
71
            return $key;
72
        }
73
        return $this->translations[$type][$key][$locale];
74
    }
75
76
    private function loadTranslationCache()
77
    {
78
        if ($this->translations === null) {
79
            $content = file_get_contents($this->translationFile);
80
            $this->translations = json_decode($content, true);
81
        }
82
    }
83
}
84