Completed
Push — develop ( 0c0d6d...6a2a65 )
by Nate
61:06
created

Flux::getScopes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/flux/license
6
 * @link       https://www.flipboxfactory.com/software/flux/
7
 */
8
9
namespace flipbox\flux;
10
11
use craft\base\Plugin;
12
use craft\web\twig\variables\CraftVariable;
13
use flipbox\ember\modules\LoggerTrait;
14
use flipbox\flux\events\RegisterScopesEvent;
15
use yii\base\Event;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 *
21
 * @property cp\Cp $cp
22
 * @property services\Transformers $transformers
23
 */
24
class Flux extends Plugin
25
{
26
    use LoggerTrait;
27
28
    /**
29
     * The global scope
30
     */
31
    const EVENT_REGISTER_SCOPES = 'registerScopes';
32
33
    /**
34
     * The global scope
35
     */
36
    const GLOBAL_SCOPE = 'global';
37
38
    /**
39
     * @var array
40
     */
41
    private $scopes;
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function init()
47
    {
48
        parent::init();
49
50
        // Modules
51
        $this->setModules([
52
            'cp' => cp\Cp::class,
53
        ]);
54
55
        // Components
56
        $this->setComponents([
57
            'transformers' => services\Transformers::class,
58
        ]);
59
60
        // Twig variables
61
        Event::on(
62
            CraftVariable::class,
63
            CraftVariable::EVENT_INIT,
64
            function (Event $event) {
65
                /** @var CraftVariable $variable */
66
                $variable = $event->sender;
67
                $variable->set('flux', static::getInstance());
68
            }
69
        );
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    protected static function getLogFileName(): string
76
    {
77
        return 'flux';
78
    }
79
80
    /**
81
     * Register scopes
82
     * @return array
83
     */
84
    public function getScopes(): array
85
    {
86
        if ($this->scopes === null) {
87
            $event = new RegisterScopesEvent([
88
                'scopes' => [
89
                    self::GLOBAL_SCOPE
90
                ]
91
            ]);
92
93
            $this->trigger(
94
                self::EVENT_REGISTER_SCOPES,
95
                $event
96
            );
97
98
            $this->scopes = $event->scopes;
99
        }
100
101
        return $this->scopes;
102
    }
103
104
    /**
105
     * @param string $scope
106
     * @return bool
107
     */
108
    public function isValidScope(string $scope): bool
109
    {
110
        return in_array($scope, $this->getScopes(), true);
111
    }
112
113
114
    /*******************************************
115
     * SERVICES
116
     *******************************************/
117
118
    /**
119
     * @noinspection PhpDocMissingThrowsInspection
120
     * @return services\Transformers
121
     */
122
    public function getTransformers(): services\Transformers
123
    {
124
        /** @noinspection PhpUnhandledExceptionInspection */
125
        /** @noinspection PhpIncompatibleReturnTypeInspection */
126
        return $this->get('transformers');
127
    }
128
}
129