Completed
Push — master ( e78df8...6c6364 )
by Nate
26:21 queued 16:45
created

TimelineEventMutator::getPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\hubspot\criteria;
10
11
use craft\helpers\StringHelper;
12
use flipbox\ember\helpers\ObjectHelper;
13
use flipbox\hubspot\HubSpot;
14
use yii\base\BaseObject;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class TimelineEventMutator extends BaseObject implements TimelineEventMutatorInterface
21
{
22
    use traits\TransformerCollectionTrait,
23
        traits\IntegrationConnectionTrait,
24
        traits\CacheTrait;
25
26
    /**
27
     * @var string
28
     */
29
    public $id;
30
31
    /**
32
     * @var string|array
33
     */
34
    public $object;
35
36
    /**
37
     * The event type Id
38
     *
39
     * @var string
40
     */
41
    public $typeId;
42
43
    /**
44
     * @var array
45
     */
46
    public $extraData = [];
47
48
    /**
49
     * @var array|null
50
     */
51
    public $payload;
52
53
    /**
54
     * @var array
55
     */
56
    public $properties = [];
57
58
    /**
59
     * @var array
60
     */
61
    public $timelineIFrame = [];
62
63
    /**
64
     * @return string
65
     */
66
    public function getTypeId(): string
67
    {
68
        return $this->typeId;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getId(): string
75
    {
76
        return (string)($this->id ?: StringHelper::randomString());
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getPayload(): array
83
    {
84
        // Explicitly set ?
85
        if ($this->payload !== null) {
86
            return (array)$this->payload;
87
        }
88
89
        $payload = array_merge(
90
            $this->getObjectPayload(),
91
            [
92
                'timelineIFrame' => $this->timelineIFrame,
93
                'extraData' => $this->extraData
94
            ],
95
            $this->properties
96
        );
97
98
        return array_filter($payload);
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    protected function getObjectPayload(): array
105
    {
106
        if (is_numeric($this->object)) {
107
            $this->object = ['objectId' => $this->object];
108
        }
109
110
        return (array)$this->object;
111
    }
112
113
    /**
114
     * @param array $config
115
     * @param null $source
116
     * @return mixed
117
     * @throws \yii\base\InvalidConfigException
118
     */
119
    public function upsert(array $config = [], $source = null)
120
    {
121
        $this->prepare($config);
122
        return HubSpot::getInstance()->getResources()->getTimelineEvents()->upsert($this, $source);
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    protected function prepare(array $criteria = [])
129
    {
130
        ObjectHelper::populate(
131
            $this,
132
            $criteria
133
        );
134
    }
135
}
136