Completed
Push — master ( b7038b...d64f6f )
by Nate
05:37 queued 03:37
created

HubSpot::getLogFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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