Completed
Push — master ( c30a0b...37c0e6 )
by Nate
08:20
created

HubSpot::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1.0034

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 8.7418
c 0
b 0
f 0
ccs 28
cts 33
cp 0.8485
cc 1
nc 1
nop 0
crap 1.0034

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\craft\hubspot;
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\Fields;
17
use craft\web\twig\variables\CraftVariable;
18
use craft\web\UrlManager;
19
use craft\web\View;
20
use flipbox\craft\ember\modules\LoggerTrait;
21
use flipbox\craft\hubspot\fields\Companies;
22
use flipbox\craft\hubspot\fields\ContactLists;
23
use flipbox\craft\hubspot\fields\Contacts;
24
use flipbox\craft\hubspot\models\Settings as SettingsModel;
25
use flipbox\craft\hubspot\web\twig\variables\HubSpot as HubSpotVariable;
26
use flipbox\craft\psr3\Logger;
27
use yii\base\Event;
28
29
/**
30
 * @author Flipbox Factory <[email protected]>
31
 * @since 1.0.0
32
 *
33
 * @method SettingsModel getSettings()
34
 *
35
 * @property services\Cache $cache
36
 * @property services\Connections $connections
37
 * @property Logger $psr3Logger
38
 */
39
class HubSpot extends Plugin
40
{
41
    use LoggerTrait;
42
43
    /**
44
     * @inheritdoc
45
     */
46
    protected static function getLogFileName(): string
47
    {
48
        return 'hubspot';
49
    }
50
51 3
    /**
52
     * @inheritdoc
53 3
     */
54
    public function init()
55
    {
56
        parent::init();
57
58
        // Components
59 3
        $this->setComponents([
60
            'cache' => services\Cache::class,
61 3
            'connections' => services\Connections::class,
62
            'psr3Logger' => function () {
63
                return Craft::createObject([
64
                    'class' => Logger::class,
65
                    'logger' => static::getLogger(),
66
                    'category' => self::getLogFileName()
67 21
                ]);
68
            }
69 21
        ]);
70
71
        // Modules
72 21
        $this->setModules([
73 21
            'cp' => cp\Cp::class
74
75 7
        ]);
76 3
77 3
        \Flipbox\HubSpot\HubSpot::setLogger(
78 3
            $this->getPsrLogger()
79 3
        );
80
81 21
        // Template variables
82
        Event::on(
83
            CraftVariable::class,
84
            CraftVariable::EVENT_INIT,
85
            function (Event $event) {
86
                /** @var CraftVariable $variable */
87
                $variable = $event->sender;
88
                $variable->set('hubspot', HubSpotVariable::class);
89 21
            }
90 21
        );
91
92
        // Integration template directory
93
        Event::on(
94
            View::class,
95 21
            View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
96 21
            function (RegisterTemplateRootsEvent $e) {
97 21
                $e->roots['flipbox/integration'] = Craft::$app->getPath()->getVendorPath() .
98 7
                    '/flipboxfactory/craft-integration/src/templates';
99
            }
100
        );
101 21
102
        // Register our field types
103
        Event::on(
104
            Fields::class,
105 21
            Fields::EVENT_REGISTER_FIELD_TYPES,
106 21
            function (RegisterComponentTypesEvent $event) {
107 21
                $event->types[] = Companies::class;
108 7
                $event->types[] = ContactLists::class;
109
                $event->types[] = Contacts::class;
110 21
            }
111
        );
112
113
        // CP routes
114 21
        Event::on(
115 21
            UrlManager::class,
116 21
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
117 7
            [self::class, 'onRegisterCpUrlRules']
118
        );
119
    }
120
121 21
    /**
122
     * @inheritdoc
123 21
     */
124
    public function getCpNavItem()
125
    {
126
        return array_merge(
127
            parent::getCpNavItem(),
128
            [
129 6
                'subnav' => [
130
                    'hubspot.settings' => [
131 6
                        'label' => static::t('Settings'),
132
                        'url' => 'hubspot/settings',
133
                    ]
134
                ]
135
            ]
136
        );
137
    }
138
139
    /**
140
     * @inheritdoc
141
     * @return SettingsModel
142
     */
143
    public function createSettingsModel()
144
    {
145
        return new SettingsModel();
146
    }
147
148
    /**
149
     * @inheritdoc
150
     * @throws \Twig_Error_Loader
151
     * @throws \yii\base\Exception
152
     */
153
    public function settingsHtml()
154 3
    {
155
        return Craft::$app->getView()->renderTemplate('hubspot/settings', [
156
            'hubspot' => $this
157
        ]);
158 3
    }
159
160
    /*******************************************
161
     * SERVICES
162
     *******************************************/
163
164
    /**
165 3
     * @noinspection PhpDocMissingThrowsInspection
166
     * @return services\Cache
167
     */
168
    public function getCache(): services\Cache
169 3
    {
170
        /** @noinspection PhpUnhandledExceptionInspection */
171
        /** @noinspection PhpIncompatibleReturnTypeInspection */
172
        return $this->get('cache');
173
    }
174
175
    /**
176 3
     * @noinspection PhpDocMissingThrowsInspection
177
     * @return services\Connections
178
     */
179
    public function getConnections(): services\Connections
180 3
    {
181
        /** @noinspection PhpUnhandledExceptionInspection */
182
        /** @noinspection PhpIncompatibleReturnTypeInspection */
183
        return $this->get('connections');
184
    }
185
186
    /**
187 3
     * @noinspection PhpDocMissingThrowsInspection
188
     * @return Logger
189
     */
190
    public function getPsrLogger(): Logger
191 3
    {
192
        /** @noinspection PhpUnhandledExceptionInspection */
193
        /** @noinspection PhpIncompatibleReturnTypeInspection */
194
        return $this->get('psr3Logger');
195
    }
196
197
198 3
    /*******************************************
199
     * TRANSLATE
200
     *******************************************/
201
202 3
    /**
203
     * Translates a message to the specified language.
204
     *
205
     * This is a shortcut method of [[\Craft::t()]].
206
     *     *
207
     * @param string $message the message to be translated.
208
     * @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
209 3
     * @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current
210
     * [[\yii\base\Application::language|application language]] will be used.
211
     * @return string the translated message.
212
     */
213 3
    public static function t($message, $params = [], $language = null)
214
    {
215
        return Craft::t('hubspot', $message, $params, $language);
216
    }
217
218
219
    /*******************************************
220
     * MODULES
221
     *******************************************/
222
223
    /**
224
     * @noinspection PhpDocMissingThrowsInspection
225 3
     * @return cp\Cp
226
     */
227
    public function getCp(): cp\Cp
228
    {
229 3
        /** @noinspection PhpUnhandledExceptionInspection */
230
        /** @noinspection PhpIncompatibleReturnTypeInspection */
231
        return $this->getModule('cp');
232
    }
233
234
235
    /*******************************************
236
     * EVENTS
237
     *******************************************/
238
239
    /**
240
     * @param RegisterUrlRulesEvent $event
241
     */
242
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
243
    {
244
        $event->rules = array_merge(
245
            $event->rules,
246
            [
247
                // ??
248
                'hubspot' => 'hubspot/cp/settings/view/general/index',
249
250
                // SETTINGS
251
                'hubspot/settings' => 'hubspot/cp/settings/view/general/index',
252
                'hubspot/settings/limits' => 'hubspot/cp/settings/view/limits/index',
253
254
                // SETTINGS: CONNECTIONS
255
                'hubspot/settings/connections' => 'hubspot/cp/settings/view/connections/index',
256
                'hubspot/settings/connections/new' => 'hubspot/cp/settings/view/connections/upsert',
257
                'hubspot/settings/connections/<identifier:\d+>' => 'hubspot/cp/settings/view/connections/upsert',
258
259
            ]
260
        );
261
    }
262
}
263