Completed
Push — master ( d64f6f...161894 )
by Nate
05:00 queued 02:19
created

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