Completed
Push — master ( 3407e1...79fe03 )
by Nate
07:26
created

SyncElementToSalesforceObjectJob::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\craft\salesforce\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\salesforce\fields\Objects;
16
17
/**
18
 * Sync a Craft Element to a Salesforce Object
19
 */
20
class SyncElementToSalesforceObjectJob 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
     * @noinspection PhpDocMissingThrowsInspection
37
     * @inheritdoc
38
     * @return bool
39
     */
40
    public function execute($queue)
41
    {
42
        $field = $this->getField();
43
44
        if (!$field instanceof Objects) {
45
            return false;
46
        }
47
48
        /** @noinspection PhpUnhandledExceptionInspection */
49
        return $field->syncToSalesforce(
50
            $this->getElement(),
0 ignored issues
show
Bug introduced by
It seems like $this->getElement() can be null; however, syncToSalesforce() 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...
51
            $this->objectId,
52
            $this->transformer
53
        );
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function serialize()
60
    {
61
        return serialize([
62
            'fieldId' => $this->getFieldId(),
63
            'elementId' => $this->getElementId(),
64
            'objectId' => $this->objectId,
65
            'transformer' => $this->transformer
66
        ]);
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function unserialize($serialized)
73
    {
74
        Craft::configure(
75
            $this,
76
            unserialize($serialized)
77
        );
78
    }
79
}
80