Completed
Push — issue#666 ( 928a8e...2654e9 )
by Guilherme
04:24
created

ScopesExtension::translateScope()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Twig\Extension;
12
13
use LoginCidadao\CoreBundle\Event\LoginCidadaoCoreEvents;
14
use LoginCidadao\CoreBundle\Event\TranslateScopeEvent;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\Translation\TranslatorInterface;
17
18
class ScopesExtension extends \Twig_Extension
19
{
20
    /** @var EventDispatcherInterface */
21
    private $dispatcher;
22
23
    /** @var TranslatorInterface */
24
    private $translator;
25
26
    /**
27
     * ScopesExtension constructor.
28
     * @param EventDispatcherInterface $dispatcher
29
     * @param TranslatorInterface $translator
30
     */
31
    public function __construct(EventDispatcherInterface $dispatcher, TranslatorInterface $translator)
32
    {
33
        $this->dispatcher = $dispatcher;
34
        $this->translator = $translator;
35
    }
36
37
    public function getFilters()
38
    {
39
        return [
40
            new \Twig_SimpleFilter('scope', [$this, 'scopeFilter']),
41
            new \Twig_SimpleFilter('scope_has_description', [$this, 'scopeHasDescription']),
42
        ];
43
    }
44
45
    public function scopeFilter($scopes, $allowBlank = false)
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...
46
    {
47
        $translator = $this->translateScope();
48
        if (is_array($scopes)) {
49
            $result = [];
50
            foreach ($scopes as $scope) {
51
                $result[$scope] = $translator($scope);
52
            }
53
54
            return $allowBlank ? $result : array_filter($result);
55
        }
56
57
        return $translator($scopes);
58
    }
59
60
    public function scopeHasDescription($scope)
0 ignored issues
show
Coding Style introduced by
function scopeHasDescription() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
61
    {
62
        $id = "scope_details.{$scope}";
63
        $translation = $this->translator->trans($id);
64
65
        return $translation !== $id;
66
    }
67
68
    private function translateScope()
69
    {
70
        return function ($scope) {
71
            $event = new TranslateScopeEvent($scope);
72
            $this->dispatcher->dispatch(LoginCidadaoCoreEvents::TRANSLATE_SCOPE, $event);
73
            if ($event->isTranslated()) {
74
                return $event->getTranslation();
75
            }
76
77
            $id = "scope.{$scope}";
78
            $translation = $this->translator->trans($id);
79
            if ($translation !== $id) {
80
                return $translation;
81
            }
82
83
            return $scope;
84
        };
85
    }
86
}
87