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

PayloadAttributeFromElementTrait::resolvePayload()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 19
cp 0
rs 9.552
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 20
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\criteria;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use craft\base\Field;
14
use craft\base\FieldInterface;
15
use flipbox\craft\salesforce\fields\Objects;
16
use flipbox\craft\salesforce\helpers\TransformerHelper;
17
use flipbox\craft\salesforce\transformers\CreateUpsertPayloadFromElement;
18
use Flipbox\HubSpot\Criteria\PayloadAttributeTrait;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.1.0
23
 */
24
trait PayloadAttributeFromElementTrait
25
{
26
    use PayloadAttributeTrait;
27
28
    /**
29
     * @var callable
30
     */
31
    public $transformer = [
32
        'class' => CreateUpsertPayloadFromElement::class,
33
        'action' => 'save'
34
    ];
35
36
    /**
37
     * @return FieldInterface|Field|null
38
     */
39
    abstract public function getField();
40
41
    /**
42
     * @return ElementInterface|Element|null
43
     */
44
    abstract public function getElement();
45
46
    /**
47
     * @return array
48
     */
49
    public function getPayload(): array
50
    {
51
        if (null === $this->payload) {
52
            $this->payload = $this->resolvePayload();
53
        }
54
55
        return $this->payload;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    protected function resolvePayload(): array
62
    {
63
        $field = $this->getField();
64
        $element = $this->getElement();
65
66
        if (!$field instanceof Objects || !$element instanceof ElementInterface) {
67
            return [];
68
        }
69
70
        if (null === ($transformer = TransformerHelper::resolveTransformer($this->transformer))) {
71
            return [];
72
        }
73
74
        $payload = call_user_func_array(
75
            $transformer,
76
            [
77
                $element,
78
                $field
79
            ]
80
        );
81
82
        return (array)$payload;
83
    }
84
}
85