Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

SyncElementToHubSpotJob   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 58
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 2
A serialize() 0 9 1
A unserialize() 0 7 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\ember\objects\ElementAttributeTrait;
14
use flipbox\craft\ember\objects\FieldAttributeTrait;
15
use flipbox\craft\hubspot\fields\ObjectsFieldInterface;
16
17
/**
18
 * Sync a Craft Element to a HubSpot Object
19
 */
20
class SyncElementToHubSpotJob extends BaseJob implements \Serializable
21
{
22
    use FieldAttributeTrait,
23
        ElementAttributeTrait;
24
25
    /**
26
     * @var string|null
27
     */
28
    public $objectId;
29
30
    /**
31
     * @var callable|array|string
32
     */
33
    public $transformer;
34
35
    /**
36
     * @inheritdoc
37
     * @return bool
38
     */
39
    public function execute($queue)
40
    {
41
        $field = $this->getField();
42
43
        if (!$field instanceof ObjectsFieldInterface) {
44
            return false;
45
        }
46
47
        return $field->syncToHubSpot(
48
            $this->getElement(),
0 ignored issues
show
Bug introduced by
It seems like $this->getElement() can be null; however, syncToHubSpot() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
49
            $this->objectId,
50
            $this->transformer
51
        );
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function serialize()
58
    {
59
        return serialize([
60
            'fieldId' => $this->getFieldId(),
61
            'elementId' => $this->getElementId(),
62
            'objectId' => $this->objectId,
63
            'transformer' => $this->transformer
64
        ]);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function unserialize($serialized)
71
    {
72
        Craft::configure(
73
            $this,
74
            unserialize($serialized)
75
        );
76
    }
77
}
78