Completed
Push — master ( 1589ee...153d10 )
by Nate
08:23
created

HubSpot   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 10
dl 0
loc 233
ccs 42
cts 70
cp 0.6
rs 10
c 0
b 0
f 0

11 Methods

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