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