Completed
Pull Request — master (#170)
by Simonas
03:06
created

SettingExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 80
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getFunctions() 0 6 1
B getSettingValue() 0 28 6
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Twig;
13
use Doctrine\Common\Cache\PhpFileCache;
14
use ONGR\SettingsBundle\Document\Setting;
15
use ONGR\SettingsBundle\Service\SettingsManager;
16
17
/**
18
 * Class SettingExtension to show settings value on twig.
19
 */
20
class SettingExtension extends \Twig_Extension
21
{
22
    /**
23
     * Extension name
24
     */
25
    const NAME = 'ongr_setting_extension';
26
27
    /**
28
     * @var SettingsManager
29
     */
30
    private $manager;
31
32
    /**
33
     * @var PhpFileCache
34
     */
35
    private $cache;
36
37
    /**
38
     * @param SettingsManager $manager
39
     * @param PhpFileCache    $cache
40
     */
41
    public function __construct($manager, $cache)
42
    {
43
        $this->manager = $manager;
44
        $this->cache = $cache;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getName()
51
    {
52
        return self::NAME;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getFunctions()
59
    {
60
        return [
61
            new \Twig_SimpleFunction('ongr_setting', [$this, 'getSettingValue']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
62
        ];
63
    }
64
65
    /**
66
     * @param string $name
67
     * @param bool   $default
68
     *
69
     * @return mixed
70
     */
71
    public function getSettingValue($name, $default = false)
72
    {
73
        $settingName = 'ongr_setting.'.$name;
74
75
        if ($this->cache->contains($settingName)) {
76
            return $this->cache->fetch($settingName);
77
        }
78
79
        $allProfiles = $this->cache->fetch('active_profiles');
80
81
        if ($allProfiles === false) {
82
            $allProfiles = $this->manager->getAllProfilesNameList(true);
83
            $this->cache->save('active_profiles', $allProfiles);
84
        }
85
86
        /** @var Setting $setting */
87
        $setting = $this->manager->get($name);
88
        $settingProfile = is_array($setting->getProfile())?$setting->getProfile():[$setting->getProfile()];
89
        if ($setting && array_intersect($settingProfile, $allProfiles)) {
90
            $settingValue = $setting->getValue();
91
        } else {
92
            $settingValue = $default;
93
        }
94
95
        $this->cache->save($settingName, $settingValue);
96
97
        return $settingValue;
98
    }
99
}
100