Completed
Push — develop ( 9a2d03...4f224f )
by Nate
09:22
created

HubSpot::getVisitor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
nc 2
nop 2
crap 6
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\web\twig\variables;
10
11
use Craft;
12
use flipbox\craft\hubspot\HubSpot as HubSpotPlugin;
13
use flipbox\craft\hubspot\models\Settings;
14
use flipbox\craft\hubspot\services\Cache;
15
use flipbox\craft\hubspot\services\Connections;
16
use yii\di\ServiceLocator;
17
use yii\helpers\Json;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class HubSpot extends ServiceLocator
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function init()
29
    {
30
        parent::init();
31
32
        $this->setComponents([
33
            'cache' => HubSpotPlugin::getInstance()->getCache(),
34
            'connections' => HubSpotPlugin::getInstance()->getConnections(),
35
            'criteria' => Criteria::class
36
        ]);
37
    }
38
39
    /**
40
     * Sub-Variables that are accessed 'craft.hubspot.settings'
41
     *
42
     * @return Settings
43
     */
44
    public function getSettings()
45
    {
46
        return HubSpotPlugin::getInstance()->getSettings();
47
    }
48
49
    /**
50
     * @noinspection PhpDocMissingThrowsInspection
51
     * @return Cache
52
     */
53
    public function getCache(): Cache
54
    {
55
        /** @noinspection PhpUnhandledExceptionInspection */
56
        /** @noinspection PhpIncompatibleReturnTypeInspection */
57
        return $this->get('cache');
58
    }
59
60
    /**
61
     * @noinspection PhpDocMissingThrowsInspection
62
     * @return Connections
63
     */
64
    public function getConnections(): Connections
65
    {
66
        /** @noinspection PhpUnhandledExceptionInspection */
67
        /** @noinspection PhpIncompatibleReturnTypeInspection */
68
        return $this->get('connections');
69
    }
70
71
    /**
72
     * @noinspection PhpDocMissingThrowsInspection
73
     * @param bool $toQueue
74
     * @param string $connection
75
     * @return array|null
76
     */
77
    public function getVisitor(bool $toQueue = true, string $connection = null)
78
    {
79
        try {
80
            return HubSpotPlugin::getInstance()->getVisitor()->findContact($toQueue, $connection);
81
        } catch (\Exception $e) {
82
83
            HubSpotPlugin::warning(
84
                sprintf(
85
                    "Exception caught while trying to get HubSpot Visitor. Exception: [%s].",
86
                    (string)Json::encode([
87
                        'Trace' => $e->getTraceAsString(),
88
                        'File' => $e->getFile(),
89
                        'Line' => $e->getLine(),
90
                        'Code' => $e->getCode(),
91
                        'Message' => $e->getMessage()
92
                    ])
93
                ),
94
                __METHOD__
95
            );
96
            return null;
97
        }
98
    }
99
}
100