Completed
Push — develop ( 62b272...366d76 )
by Nate
03:03
created

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