Completed
Push — develop ( 0d1acc...d6637f )
by Nate
03:44
created

HubSpot::getCp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 a table
119 9
        ObjectAssociation::ensureEnvironmentTableExists();
120 6
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function getCpNavItem()
126
    {
127
        return array_merge(
128
            parent::getCpNavItem(),
129
            [
130
                'subnav' => [
131
                    'hubspot.settings' => [
132
                        'label' => static::t('Settings'),
133
                        'url' => 'hubspot/settings',
134
                    ]
135
                ]
136
            ]
137
        );
138
    }
139
140
    /**
141
     * @inheritdoc
142
     * @return SettingsModel
143
     */
144 6
    public function createSettingsModel()
145
    {
146 6
        return new SettingsModel();
147
    }
148
149
    /**
150
     * @inheritdoc
151
     * @throws \Twig_Error_Loader
152
     * @throws \yii\base\Exception
153
     */
154
    public function settingsHtml()
155
    {
156
        return Craft::$app->getView()->renderTemplate('hubspot/settings', [
157
            'hubspot' => $this
158
        ]);
159
    }
160
161
    /*******************************************
162
     * SERVICES
163
     *******************************************/
164
165
    /**
166
     * @noinspection PhpDocMissingThrowsInspection
167
     * @return services\Cache
168
     */
169
    public function getCache(): services\Cache
170
    {
171
        /** @noinspection PhpUnhandledExceptionInspection */
172
        /** @noinspection PhpIncompatibleReturnTypeInspection */
173
        return $this->get('cache');
174
    }
175
176
    /**
177
     * @noinspection PhpDocMissingThrowsInspection
178
     * @return services\Connections
179
     */
180 3
    public function getConnections(): services\Connections
181
    {
182
        /** @noinspection PhpUnhandledExceptionInspection */
183
        /** @noinspection PhpIncompatibleReturnTypeInspection */
184 3
        return $this->get('connections');
185
    }
186
187
    /**
188
     * @noinspection PhpDocMissingThrowsInspection
189
     * @return Logger
190
     */
191 9
    public function getPsrLogger(): Logger
192
    {
193
        /** @noinspection PhpUnhandledExceptionInspection */
194
        /** @noinspection PhpIncompatibleReturnTypeInspection */
195 9
        return $this->get('psr3Logger');
196
    }
197
198
199
    /*******************************************
200
     * TRANSLATE
201
     *******************************************/
202
203
    /**
204
     * Translates a message to the specified language.
205
     *
206
     * This is a shortcut method of [[\Craft::t()]].
207
     *     *
208
     * @param string $message the message to be translated.
209
     * @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
210
     * @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current
211
     * [[\yii\base\Application::language|application language]] will be used.
212
     * @return string the translated message.
213
     */
214
    public static function t($message, $params = [], $language = null)
215
    {
216
        return Craft::t('hubspot', $message, $params, $language);
217
    }
218
219
220
    /*******************************************
221
     * MODULES
222
     *******************************************/
223
224
    /**
225
     * @noinspection PhpDocMissingThrowsInspection
226
     * @return cp\Cp
227
     */
228
    public function getCp(): cp\Cp
229
    {
230
        /** @noinspection PhpUnhandledExceptionInspection */
231
        /** @noinspection PhpIncompatibleReturnTypeInspection */
232
        return $this->getModule('cp');
233
    }
234
235
236
    /*******************************************
237
     * EVENTS
238
     *******************************************/
239
240
    /**
241
     * @param RegisterUrlRulesEvent $event
242
     */
243
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
244
    {
245
        $event->rules = array_merge(
246
            $event->rules,
247
            [
248
                // ??
249
                'hubspot' => 'hubspot/cp/settings/view/general/index',
250
251
                // SETTINGS
252
                'hubspot/settings' => 'hubspot/cp/settings/view/general/index',
253
                'hubspot/settings/limits' => 'hubspot/cp/settings/view/limits/index',
254
255
                // SETTINGS: CONNECTIONS
256
                'hubspot/settings/connections' => 'hubspot/cp/settings/view/connections/index',
257
                'hubspot/settings/connections/new' => 'hubspot/cp/settings/view/connections/upsert',
258
                'hubspot/settings/connections/<identifier:\d+>' => 'hubspot/cp/settings/view/connections/upsert',
259
260
            ]
261
        );
262
    }
263
}
264