PreferencesModulePlugin::getFunctions()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 12
nc 1
nop 0
1
<?php namespace Anomaly\PreferencesModule;
2
3
use Anomaly\PreferencesModule\Preference\Command\GetPreference;
4
use Anomaly\PreferencesModule\Preference\Command\GetPreferenceValue;
5
use Anomaly\PreferencesModule\Preference\Command\GetValueFieldType;
6
use Anomaly\PreferencesModule\Preference\Contract\PreferenceInterface;
7
use Anomaly\Streams\Platform\Addon\Plugin\Plugin;
8
use Anomaly\Streams\Platform\Support\Decorator;
9
10
/**
11
 * Class PreferencesModulePlugin
12
 *
13
 * @link          http://pyrocms.com/
14
 * @author        PyroCMS, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 */
17
class PreferencesModulePlugin extends Plugin
18
{
19
20
    /**
21
     * Get the functions.
22
     *
23
     * @return array
24
     */
25
    public function getFunctions()
26
    {
27
        return [
28
            new \Twig_SimpleFunction(
29
                'preference_value',
30
                function ($key) {
31
                    return $this->dispatch(new GetPreferenceValue($key));
32
                }
33
            ),
34
            new \Twig_SimpleFunction(
35
                'preference',
36
                function ($key) {
37
38
                    /* @var PreferenceInterface $preference */
39
                    if (!$preference = $this->dispatch(new GetPreference($key))) {
40
                        return null;
41
                    }
42
43
                    return (new Decorator())->decorate($this->dispatch(new GetValueFieldType($preference)));
44
                }
45
            ),
46
        ];
47
    }
48
}
49