Completed
Push — develop ( 856b14...45ab37 )
by Nate
17:44
created

Patron::getLogFileName()   A

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/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron;
10
11
use Craft;
12
use craft\base\Plugin;
13
use craft\events\RegisterUrlRulesEvent;
14
use craft\helpers\UrlHelper;
15
use craft\web\twig\variables\CraftVariable;
16
use craft\web\UrlManager;
17
use flipbox\ember\modules\LoggerTrait;
18
use flipbox\patron\migrations\m181019_220655_provider_instances;
19
use flipbox\patron\models\Settings as SettingsModel;
20
use yii\base\Event;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 *
26
 * @method SettingsModel getSettings()
27
 *
28
 * @property services\Providers $providers
29
 * @property services\ProviderSettings $providerSettings
30
 * @property services\ProviderLocks $providerLocks
31
 * @property services\Tokens $tokens
32
 * @property services\ManageProviders $manageProviders
33
 * @property services\ManageTokens $manageTokens
34
 * @property services\Session $session
35
 */
36
class Patron extends Plugin
37
{
38
    use LoggerTrait;
39
40
    /**
41
     * @return string
42
     */
43
    protected static function getLogFileName(): string
44
    {
45
        return 'patron';
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 18
    public function init()
52
    {
53 18
        parent::init();
54
55
        // Components
56 18
        $this->setComponents([
57 18
            'providers' => services\Providers::class,
58
            'providerSettings' => services\ProviderSettings::class,
59
            'providerLocks' => services\ProviderLocks::class,
60
            'tokens' => services\Tokens::class,
61
            'manageProviders' => services\ManageProviders::class,
62
            'manageTokens' => services\ManageTokens::class,
63
            'session' => services\Session::class
64
        ]);
65
66
        // Modules
67 18
        $this->setModules([
68 18
            'cp' => cp\Cp::class
69
70
        ]);
71
72
        // Template variables
73 18
        Event::on(
74 18
            CraftVariable::class,
75 18
            CraftVariable::EVENT_INIT,
76 6
            function (Event $event) {
77
                /** @var CraftVariable $variable */
78
                $variable = $event->sender;
79
                $variable->set('patron', self::getInstance());
80 18
            }
81
        );
82
83
        // CP routes
84 18
        Event::on(
85 18
            UrlManager::class,
86 18
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
87 18
            [self::class, 'onRegisterCpUrlRules']
88
        );
89
90
        // Register our site routes
91 18
        Event::on(
92 18
            UrlManager::class,
93 18
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
94 6
            function (RegisterUrlRulesEvent $event) {
95
                if ($callbackUrlRule = $this->getSettings()->getCallbackUrlRule()) {
96
                    $event->rules = array_merge(
97
                        $event->rules,
98
                        $callbackUrlRule
99
                    );
100
                }
101 18
            }
102
        );
103
104
        // Add default environments upon creation
105 18
        $defaultEnvironments = $this->getSettings()->getDefaultEnvironments();
106 18
        if (!empty($defaultEnvironments)) {
107 18
            Event::on(
108 18
                records\ProviderInstance::class,
109 18
                records\ProviderInstance::EVENT_BEFORE_INSERT,
110
                [
111 18
                    events\handlers\BeforeInsertProviderInstance::class,
112
                    'handle'
113
                ]
114
            );
115
        }
116
117
        // Replicate environments to token
118 18
        if ($this->getSettings()->applyProviderEnvironmentsToToken === true) {
119 18
            Event::on(
120 18
                records\Token::class,
121 18
                records\Token::EVENT_BEFORE_INSERT,
122
                [
123 18
                    events\handlers\BeforeInsertToken::class,
124
                    'handle'
125
                ]
126
            );
127
        }
128 18
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function getCpNavItem()
134
    {
135
        return array_merge(
136
            parent::getCpNavItem(),
137
            [
138
                'subnav' => [
139
                    'patron.providers' => [
140
                        'label' => Craft::t('patron', 'Providers'),
141
                        'url' => 'patron/providers',
142
                    ],
143
                    'patron.settings' => [
144
                        'label' => Craft::t('patron', 'Settings'),
145
                        'url' => 'patron/settings',
146
                    ]
147
                ]
148
            ]
149
        );
150
    }
151
152
    /**
153
     * @inheritdoc
154
     * @return SettingsModel
155
     */
156 18
    public function createSettingsModel()
157
    {
158 18
        return new SettingsModel();
159
    }
160
161
    /**
162
     * @inheritdoc
163
     * @throws \yii\base\ExitException
164
     */
165
    public function getSettingsResponse()
166
    {
167
        Craft::$app->getResponse()->redirect(
168
            UrlHelper::cpUrl('patron/settings')
169
        );
170
171
        Craft::$app->end();
172
    }
173
174
    /*******************************************
175
     * SERVICES
176
     *******************************************/
177
178
    /**
179
     * @noinspection PhpDocMissingThrowsInspection
180
     * @return services\Providers
181
     */
182 3
    public function getProviders(): services\Providers
183
    {
184
        /** @noinspection PhpUnhandledExceptionInspection */
185
        /** @noinspection PhpIncompatibleReturnTypeInspection */
186 3
        return $this->get('providers');
187
    }
188
189
    /**
190
     * @noinspection PhpDocMissingThrowsInspection
191
     * @return services\ProviderSettings
192
     */
193
    public function getProviderSettings(): services\ProviderSettings
194
    {
195
        /** @noinspection PhpUnhandledExceptionInspection */
196
        /** @noinspection PhpIncompatibleReturnTypeInspection */
197
        return $this->get('providerSettings');
198
    }
199
200
    /**
201
     * @noinspection PhpDocMissingThrowsInspection
202
     * @return services\ProviderLocks
203
     */
204 3
    public function getProviderLocks(): services\ProviderLocks
205
    {
206
        /** @noinspection PhpUnhandledExceptionInspection */
207
        /** @noinspection PhpIncompatibleReturnTypeInspection */
208 3
        return $this->get('providerLocks');
209
    }
210
211
    /**
212
     * @noinspection PhpDocMissingThrowsInspection
213
     * @return services\Tokens
214
     */
215 3
    public function getTokens(): services\Tokens
216
    {
217
        /** @noinspection PhpUnhandledExceptionInspection */
218
        /** @noinspection PhpIncompatibleReturnTypeInspection */
219 3
        return $this->get('tokens');
220
    }
221
222
    /**
223
     * @noinspection PhpDocMissingThrowsInspection
224
     * @return services\ManageProviders
225
     */
226 3
    public function manageProviders(): services\ManageProviders
227
    {
228
        /** @noinspection PhpUnhandledExceptionInspection */
229
        /** @noinspection PhpIncompatibleReturnTypeInspection */
230 3
        return $this->get('manageProviders');
231
    }
232
233
    /**
234
     * @noinspection PhpDocMissingThrowsInspection
235
     * @return services\ManageTokens
236
     */
237 3
    public function manageTokens(): services\ManageTokens
238
    {
239
        /** @noinspection PhpUnhandledExceptionInspection */
240
        /** @noinspection PhpIncompatibleReturnTypeInspection */
241 3
        return $this->get('manageTokens');
242
    }
243
244
    /**
245
     * @noinspection PhpDocMissingThrowsInspection
246
     * @return services\Session
247
     */
248 3
    public function getSession(): services\Session
249
    {
250
        /** @noinspection PhpUnhandledExceptionInspection */
251
        /** @noinspection PhpIncompatibleReturnTypeInspection */
252 3
        return $this->get('session');
253
    }
254
255
256
    /*******************************************
257
     * MODULES
258
     *******************************************/
259
260
    /**
261
     * @noinspection PhpDocMissingThrowsInspection
262
     * @return cp\Cp
263
     */
264
    public function getCp(): cp\Cp
265
    {
266
        /** @noinspection PhpUnhandledExceptionInspection */
267
        /** @noinspection PhpIncompatibleReturnTypeInspection */
268
        return $this->getModule('cp');
269
    }
270
271
    /*******************************************
272
     * EVENTS
273
     *******************************************/
274
275
    /**
276
     * @param RegisterUrlRulesEvent $event
277
     */
278
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
279
    {
280
        $event->rules = array_merge(
281
            $event->rules,
282
            [
283
                // SETTINGS
284
                'patron/settings' => 'patron/cp/view/settings/index',
285
286
                'patron' =>
287
                    'patron/cp/view/general/index',
288
289
                'patron/providers' =>
290
                    'patron/cp/view/providers/default/index',
291
292
                'patron/providers/new' =>
293
                    'patron/cp/view/providers/default/upsert',
294
295
                'patron/providers/<identifier:\d+>' =>
296
                    'patron/cp/view/providers/default/upsert',
297
298
                'patron/providers/<provider:\d+>/instances/<identifier:\d+>' =>
299
                    'patron/cp/view/providers/instances/upsert',
300
301
                'patron/providers/<provider:\d+>/instances/new' =>
302
                    'patron/cp/view/providers/instances/upsert',
303
304
                'patron/providers/<provider:\d+>/tokens' =>
305
                    'patron/cp/view/providers/tokens/index',
306
            ]
307
        );
308
    }
309
}
310