Completed
Push — develop ( 94b72f...27afa3 )
by Nate
03:15
created

Patron::getCp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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