Completed
Push — develop ( ed5f7e...bc4d8c )
by Nate
12:09 queued 10:24
created

Patron   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 47.83%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 7
dl 0
loc 250
ccs 33
cts 69
cp 0.4783
rs 10
c 0
b 0
f 0

11 Methods

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