Flux::getLogFileName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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