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

SaveVisitor::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 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\queue;
10
11
use Craft;
12
use craft\queue\BaseJob;
13
use flipbox\craft\hubspot\HubSpot;
14
use flipbox\craft\hubspot\records\Visitor as VisitorRecord;
15
16
/**
17
 * Sync a HubSpot Object to a Craft Element
18
 */
19
class SaveVisitor extends BaseJob implements \Serializable
20
{
21
    /**
22
     * The default description
23
     */
24
    const DESCRIPTION = "Saving HubSpot Visitor by Token: ";
25
26
    /**
27
     * @var string|null
28
     */
29
    public $token;
30
31
    /**
32
     * The HubSpot connection identifier
33
     *
34
     * @var string|null
35
     */
36
    public $connection;
37
38
    /**
39
     * Returns a default description for [[getDescription()]].
40
     *
41
     * @return string|null
42
     */
43
    protected function defaultDescription()
44
    {
45
        return static::DESCRIPTION . $this->token;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     * @throws \Exception
51
     */
52
    public function execute($queue)
53
    {
54
        if (empty($this->token)) {
55
            return;
56
        }
57
58
        // Sync
59
        HubSpot::getInstance()->getVisitor()->syncFromHubSpot(
60
            VisitorRecord::findOrCreate(
61
                $this->token,
62
                $this->connection
63
            )
64
        );
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function serialize()
71
    {
72
        return serialize([
73
            'token' => $this->token,
74
            'connection' => $this->connection
75
        ]);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function unserialize($serialized)
82
    {
83
        Craft::configure(
84
            $this,
85
            unserialize($serialized)
86
        );
87
    }
88
}
89