Completed
Push — master ( 1fe708...6360f1 )
by Arnaud
16s queued 12s
created

AdminExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 53
ccs 17
cts 19
cp 0.8947
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A isTranslationEnabled() 0 3 1
A isAdminActionAllowed() 0 3 1
A getApplicationParameter() 0 5 1
A isMediaBundleEnabled() 0 3 1
A getFunctions() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Twig\Extension;
6
7
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
8
use LAG\AdminBundle\Security\Helper\SecurityHelper;
9
use Twig\Extension\AbstractExtension;
10
use Twig\TwigFunction;
11
12
class AdminExtension extends AbstractExtension
13
{
14
    private bool $mediaEnabled;
15
    private ApplicationConfiguration $appConfig;
16
    private SecurityHelper $security;
17
18 5
    public function __construct(
19
        bool $mediaEnabled,
20
        ApplicationConfiguration $appConfig,
21
        SecurityHelper $security
22
    ) {
23 5
        $this->appConfig = $appConfig;
24 5
        $this->security = $security;
25 5
        $this->mediaEnabled = $mediaEnabled;
26 5
    }
27
28 1
    public function getFunctions(): array
29
    {
30
        return [
31 1
            new TwigFunction('admin_config', [$this, 'getApplicationParameter']),
32 1
            new TwigFunction('admin_action_allowed', [$this, 'isAdminActionAllowed']),
33 1
            new TwigFunction('admin_media_enabled', [$this, 'isMediaBundleEnabled']),
34 1
            new TwigFunction('admin_is_translation_enabled', [$this, 'isTranslationEnabled']),
35
        ];
36
    }
37
38 1
    public function getApplicationParameter($name)
39
    {
40
        return $this
41 1
            ->appConfig
42 1
            ->get($name)
43
        ;
44
    }
45
46
    /**
47
     * Return true if the given action is allowed for the given Admin.
48
     */
49 1
    public function isAdminActionAllowed(string $adminName, string $actionName): bool
50
    {
51 1
        return $this->security->isActionAllowed($adminName, $actionName);
52
    }
53
54
    /**
55
     * Return true if the media bundle is enabled.
56
     */
57 1
    public function isMediaBundleEnabled(): bool
58
    {
59 1
        return $this->mediaEnabled;
60
    }
61
62
    public function isTranslationEnabled(): bool
63
    {
64
        return $this->appConfig->isTranslationEnabled();
65
    }
66
}
67