Passed
Push — master ( 3fcb32...49d6fc )
by Julito
09:27
created

ChamiloExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 85
rs 10
c 1
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSettingsParameter() 0 3 1
A getFilters() 0 21 1
A completeUserNameWithLink() 0 12 1
A __construct() 0 6 1
A getIllustration() 0 3 1
A getFunctions() 0 6 1
A getSettings() 0 3 1
A getName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Twig\Extension;
8
9
use Chamilo\CoreBundle\Component\Utils\NameConvention;
10
use Chamilo\CoreBundle\Entity\ResourceIllustrationInterface;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
13
use Chamilo\CoreBundle\Twig\SettingsHelper;
14
use Sylius\Bundle\SettingsBundle\Model\SettingsInterface;
15
use Symfony\Component\Routing\RouterInterface;
16
use Twig\Extension\AbstractExtension;
17
use Twig\TwigFilter;
18
use Twig\TwigFunction;
19
20
class ChamiloExtension extends AbstractExtension
21
{
22
    private IllustrationRepository $illustrationRepository;
23
    private SettingsHelper $helper;
24
    private RouterInterface $router;
25
    private NameConvention $nameConvention;
26
27
    public function __construct(IllustrationRepository $illustrationRepository, SettingsHelper $helper, RouterInterface $router, NameConvention $nameConvention)
28
    {
29
        $this->illustrationRepository = $illustrationRepository;
30
        $this->helper = $helper;
31
        $this->router = $router;
32
        $this->nameConvention = $nameConvention;
33
    }
34
35
    public function getFilters(): array
36
    {
37
        return [
38
            new TwigFilter('var_dump', 'var_dump'),
39
            new TwigFilter('icon', 'Display::get_icon_path'),
40
            new TwigFilter('get_lang', 'get_lang'),
41
            new TwigFilter('get_plugin_lang', 'get_plugin_lang'),
42
            new TwigFilter('icon', 'Display::get_icon_path'),
43
            new TwigFilter('img', 'Display::get_image'),
44
            new TwigFilter('api_get_local_time', 'api_get_local_time'),
45
            new TwigFilter('api_convert_and_format_date', 'api_convert_and_format_date'),
46
            new TwigFilter('format_date', 'api_format_date'),
47
            new TwigFilter('format_file_size', 'format_file_size'),
48
            new TwigFilter('date_to_time_ago', 'Display::dateToStringAgoAndLongDate'),
49
            new TwigFilter('api_get_configuration_value', 'api_get_configuration_value'),
50
            new TwigFilter('remove_xss', 'Security::remove_XSS'),
51
            new TwigFilter('user_complete_name', 'UserManager::formatUserFullName'),
52
            new TwigFilter('user_complete_name_with_link', [$this, 'completeUserNameWithLink']),
53
            new TwigFilter('illustration', [$this, 'getIllustration']),
54
55
            new TwigFilter('api_get_setting', [$this, 'getSettingsParameter']),
56
        ];
57
    }
58
59
    public function getFunctions()
60
    {
61
        return [
62
            new TwigFunction('chamilo_settings_all', [$this, 'getSettings']),
63
            new TwigFunction('chamilo_settings_get', [$this, 'getSettingsParameter']),
64
            new TwigFunction('chamilo_settings_has', [$this, 'hasSettingsParameter']),
65
        ];
66
    }
67
68
    public function completeUserNameWithLink(User $user): string
69
    {
70
        $url = $this->router->generate(
71
            'legacy_main',
72
            [
73
                'name' => '/inc/ajax/user_manager.ajax.php?a=get_user_popup&user_id='.$user->getId(),
74
            ]
75
        );
76
77
        $name = $this->nameConvention->getPersonName($user);
78
79
        return "<a href=\"$url\" class=\"ajax\">$name</a>";
80
    }
81
82
    public function getIllustration(ResourceIllustrationInterface $resource): string
83
    {
84
        return $this->illustrationRepository->getIllustrationUrl($resource);
85
    }
86
87
    public function getSettings($namespace): SettingsInterface
88
    {
89
        return $this->helper->getSettings($namespace);
90
    }
91
92
    public function getSettingsParameter($name)
93
    {
94
        return $this->helper->getSettingsParameter($name);
95
    }
96
97
    /**
98
     * Returns the name of the extension.
99
     *
100
     * @return string The extension name
101
     */
102
    public function getName()
103
    {
104
        return 'chamilo_extension';
105
    }
106
}
107