Completed
Push — master ( 37c0e6...5c203e )
by Nate
08:56 queued 06:36
created

HubSpot   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 61.76%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 10
dl 0
loc 224
ccs 42
cts 68
cp 0.6176
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 14 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 20 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(),
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.settings' => [
131
                        '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
    {
155
        return Craft::$app->getView()->renderTemplate('hubspot/settings', [
156
            'hubspot' => $this
157
        ]);
158
    }
159
160
    /*******************************************
161
     * SERVICES
162
     *******************************************/
163
164
    /**
165
     * @noinspection PhpDocMissingThrowsInspection
166
     * @return services\Cache
167
     */
168 3
    public function getCache(): services\Cache
169
    {
170
        /** @noinspection PhpUnhandledExceptionInspection */
171
        /** @noinspection PhpIncompatibleReturnTypeInspection */
172 3
        return $this->get('cache');
173
    }
174
175
    /**
176
     * @noinspection PhpDocMissingThrowsInspection
177
     * @return services\Connections
178
     */
179 3
    public function getConnections(): services\Connections
180
    {
181
        /** @noinspection PhpUnhandledExceptionInspection */
182
        /** @noinspection PhpIncompatibleReturnTypeInspection */
183 3
        return $this->get('connections');
184
    }
185
186
    /**
187
     * @noinspection PhpDocMissingThrowsInspection
188
     * @return Logger
189
     */
190 9
    public function getPsrLogger(): Logger
191
    {
192
        /** @noinspection PhpUnhandledExceptionInspection */
193
        /** @noinspection PhpIncompatibleReturnTypeInspection */
194 9
        return $this->get('psr3Logger');
195
    }
196
197
198
    /*******************************************
199
     * TRANSLATE
200
     *******************************************/
201
202
    /**
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
     * @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
    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
     * @return cp\Cp
226
     */
227
    public function getCp(): cp\Cp
228
    {
229
        /** @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