Completed
Push — master ( e1d59a...95b59d )
by Nate
07:06
created

Force::getConnections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\craft\salesforce;
10
11
use Craft;
12
use craft\base\Plugin;
13
use craft\events\RegisterComponentTypesEvent;
14
use craft\events\RegisterTemplateRootsEvent;
15
use craft\events\RegisterUrlRulesEvent;
16
use craft\services\Dashboard;
17
use craft\services\Fields;
18
use craft\web\twig\variables\CraftVariable;
19
use craft\web\UrlManager;
20
use craft\web\View;
21
use flipbox\craft\ember\helpers\UrlHelper;
22
use flipbox\craft\ember\modules\LoggerTrait;
23
use flipbox\craft\psr3\Logger;
24
use flipbox\craft\salesforce\fields\Objects as ObjectsField;
25
use flipbox\craft\salesforce\models\Settings as SettingsModel;
26
use flipbox\craft\salesforce\web\twig\Extension;
27
use flipbox\craft\salesforce\web\twig\variables\Force as ForceVariable;
28
use Flipbox\Salesforce\Salesforce;
29
use yii\base\Event;
30
31
/**
32
 * @author Flipbox Factory <[email protected]>
33
 * @since 1.0.0
34
 *
35
 * @method SettingsModel getSettings()
36
 *
37
 * @property services\Cache $cache
38
 * @property services\Connections $connections
39
 * @property Logger $psr3Logger
40
 */
41
class Force extends Plugin
42
{
43
    use LoggerTrait;
44
45
    /**
46
     * @var string
47
     */
48
    public static $category = 'salesforce';
49
50
    /**
51
     * @inheritdocfind
52
     */
53
    public function init()
54
    {
55
        parent::init();
56
57
        // Components
58
        $this->setComponents([
59
            'cache' => services\Cache::class,
60
            'connections' => services\Connections::class,
61
            'psr3Logger' => function () {
62
                return Craft::createObject([
63
                    'class' => Logger::class,
64
                    'logger' => static::getLogger(),
0 ignored issues
show
Deprecated Code introduced by
The method flipbox\craft\ember\modu...oggerTrait::getLogger() has been deprecated.

This method has been deprecated.

Loading history...
65
                    'category' => self::getLogFileName()
66
                ]);
67
            }
68
        ]);
69
70
        // Pass logger along to package
71
        Salesforce::setLogger(
72
            static::getPsrLogger()
73
        );
74
75
        // Modules
76
        $this->setModules([
77
            'cp' => cp\Cp::class
78
        ]);
79
80
        // Add our custom twig extension
81
        Craft::$app->getView()->getTwig()->addExtension(new Extension());
82
83
        // Fields
84
        Event::on(
85
            Fields::class,
86
            Fields::EVENT_REGISTER_FIELD_TYPES,
87
            function (RegisterComponentTypesEvent $event) {
88
                $event->types[] = ObjectsField::class;
89
            }
90
        );
91
92
        // Register our widgets
93
        Event::on(
94
            Dashboard::class,
95
            Dashboard::EVENT_REGISTER_WIDGET_TYPES,
96
            function (RegisterComponentTypesEvent $event) {
97
                $event->types[] = widgets\ObjectWidget::class;
98
            }
99
        );
100
101
        // Template variables
102
        Event::on(
103
            CraftVariable::class,
104
            CraftVariable::EVENT_INIT,
105
            function (Event $event) {
106
                /** @var CraftVariable $variable */
107
                $variable = $event->sender;
108
                $variable->set('salesforce', ForceVariable::class);
109
            }
110
        );
111
112
        // Integration template directory
113
        Event::on(
114
            View::class,
115
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
116
            function (RegisterTemplateRootsEvent $e) {
117
                $e->roots['flipbox/integration'] = Craft::$app->getPath()->getVendorPath() .
118
                    '/flipboxfactory/craft-integration/src/templates';
119
            }
120
        );
121
122
        // CP routes
123
        Event::on(
124
            UrlManager::class,
125
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
126
            [self::class, 'onRegisterCpUrlRules']
127
        );
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    protected static function getLogFileName(): string
134
    {
135
        return 'salesforce';
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function getCpNavItem()
142
    {
143
        return array_merge(
144
            parent::getCpNavItem(),
145
            [
146
                'subnav' => [
147
                    'salesforce.queries' => [
148
                        'label' => static::t('Queries'),
149
                        'url' => 'salesforce/queries'
150
                    ],
151
                    'salesforce.objects' => [
152
                        'label' => static::t('Objects'),
153
                        'url' => 'salesforce/objects'
154
                    ],
155
                    'salesforce.limits' => [
156
                        'label' => static::t('Limits'),
157
                        'url' => 'salesforce/limits'
158
                    ],
159
                    'salesforce.settings' => [
160
                        'label' => static::t('Settings'),
161
                        'url' => 'salesforce/settings',
162
                    ]
163
                ]
164
            ]
165
        );
166
    }
167
168
    /*******************************************
169
     * SETTINGS
170
     *******************************************/
171
172
    /**
173
     * @inheritdoc
174
     * @return SettingsModel
175
     */
176
    public function createSettingsModel()
177
    {
178
        return new SettingsModel();
179
    }
180
181
    /**
182
     * @inheritdoc
183
     * @throws \yii\base\ExitException
184
     */
185
    public function getSettingsResponse()
186
    {
187
        Craft::$app->getResponse()->redirect(
188
            UrlHelper::cpUrl('salesforce/settings')
189
        );
190
191
        Craft::$app->end();
192
    }
193
194
195
    /*******************************************
196
     * SERVICES
197
     *******************************************/
198
199
    /**
200
     * @noinspection PhpDocMissingThrowsInspection
201
     * @return services\Cache
202
     */
203 3
    public function getCache(): services\Cache
204
    {
205
        /** @noinspection PhpUnhandledExceptionInspection */
206
        /** @noinspection PhpIncompatibleReturnTypeInspection */
207 3
        return $this->get('cache');
208
    }
209
210
    /**
211
     * @noinspection PhpDocMissingThrowsInspection
212
     * @return services\Connections
213
     */
214 3
    public function getConnections(): services\Connections
215
    {
216
        /** @noinspection PhpUnhandledExceptionInspection */
217
        /** @noinspection PhpIncompatibleReturnTypeInspection */
218 3
        return $this->get('connections');
219
    }
220
221
    /**
222
     * @noinspection PhpDocMissingThrowsInspection
223
     * @return Logger
224
     */
225 3
    public function getPsrLogger(): Logger
226
    {
227
        /** @noinspection PhpUnhandledExceptionInspection */
228
        /** @noinspection PhpIncompatibleReturnTypeInspection */
229 3
        return $this->get('psr3Logger');
230
    }
231
232
233
    /*******************************************
234
     * MODULES
235
     *******************************************/
236
237
    /**
238
     * @noinspection PhpDocMissingThrowsInspection
239
     * @return cp\Cp
240
     */
241 3
    public function getCp(): cp\Cp
242
    {
243
        /** @noinspection PhpUnhandledExceptionInspection */
244
        /** @noinspection PhpIncompatibleReturnTypeInspection */
245 3
        return $this->getModule('cp');
246
    }
247
248
249
    /*******************************************
250
     * TRANSLATE
251
     *******************************************/
252
253
    /**
254
     * Translates a message to the specified language.
255
     *
256
     * This is a shortcut method of [[\Craft::t()]].
257
     *     *
258
     * @param string $message the message to be translated.
259
     * @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
260
     * @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current
261
     * [[\yii\base\Application::language|application language]] will be used.
262
     * @return string the translated message.
263
     */
264
    public static function t($message, $params = [], $language = null)
265
    {
266
        return Craft::t('salesforce', $message, $params, $language);
267
    }
268
269
270
    /*******************************************
271
     * EVENTS
272
     *******************************************/
273
274
    /**
275
     * @param RegisterUrlRulesEvent $event
276
     */
277
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
278
    {
279
        $event->rules = array_merge(
280
            $event->rules,
281
            [
282
                // ??
283
                'salesforce' => 'salesforce/cp/view/queries/index',
284
285
                // QUERIES
286
                'salesforce/queries' => 'salesforce/cp/view/queries/index',
287
                'salesforce/queries/new' => 'salesforce/cp/view/queries/upsert',
288
                'salesforce/queries/<identifier:\d+>' => 'salesforce/cp/view/queries/upsert',
289
290
                // LIMITS
291
                'salesforce/limits' => 'salesforce/cp/view/limits/index',
292
293
                // SOBJECTS
294
                'salesforce/objects' => 'salesforce/cp/view/objects/index',
295
                'salesforce/objects/payloads/<field:\d+>/element/<element:\d+>' =>
296
                    'salesforce/cp/view/object-payloads/index',
297
298
                // SETTINGS
299
                'salesforce/settings' => 'salesforce/cp/settings/view/general/index',
300
301
                // SETTINGS: CONNECTIONS
302
                'salesforce/settings/connections' => 'salesforce/cp/settings/view/connections/index',
303
                'salesforce/settings/connections/new' => 'salesforce/cp/settings/view/connections/upsert',
304
                'salesforce/settings/connections/<identifier:\d+>' => 'salesforce/cp/settings/view/connections/upsert',
305
            ]
306
        );
307
    }
308
}
309