SettingsExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 6 1
A getSetting() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Harentius\BlogBundle\Twig;
6
7
use Harentius\BlogBundle\Entity\SettingRepository;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
class SettingsExtension extends AbstractExtension
12
{
13
    /**
14
     * @var SettingRepository
15
     */
16
    private $settingRepository;
17
18
    /**
19
     * @param SettingRepository $settingRepository
20
     */
21
    public function __construct(SettingRepository $settingRepository)
22
    {
23
        $this->settingRepository = $settingRepository;
24
    }
25
26
    /**
27
     * @return array
28
     */
29
    public function getFunctions()
30
    {
31
        return [
32
            new TwigFunction('get_setting', [$this, 'getSetting']),
33
        ];
34
    }
35
36
    /**
37
     * @param string $key
38
     * @return string|null
39
     */
40
    public function getSetting(string $key): ?string
41
    {
42
        $setting = $this->settingRepository->findOneBy(['key' => $key]);
43
44
        return $setting ? $setting->getValue() : null;
45
    }
46
}
47