Completed
Push — develop ( 3644a8...72e19e )
by Nate
06:59
created

TimelineEventCriteria::getContactPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 TimelineEventCriteria extends BaseObject implements TimelineEventCriteriaInterface
21
{
22
    use traits\TransformerCollectionTrait,
23
        traits\IntegrationConnectionTrait,
24
        traits\CacheTrait;
25
26
    /**
27
     * The event Id
28
     *
29
     * @var string
30
     */
31
    public $id;
32
33
    /**
34
     * @var string|array
35
     */
36
    public $object;
37
38
    /**
39
     * The event type Id
40
     *
41
     * @var string
42
     */
43
    public $typeId;
44
45
    /**
46
     * @var array
47
     */
48
    public $extraData = [];
49
50
    /**
51
     * @var array|null
52
     */
53
    public $payload;
54
55
    /**
56
     * @var array
57
     */
58
    public $properties = [];
59
60
    /**
61
     * @var array
62
     */
63
    public $timelineIFrame = [];
64
65
    /**
66
     * @return string
67
     */
68
    public function getTypeId(): string
69
    {
70
        return $this->typeId;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getId(): string
77
    {
78
        return (string)($this->id ?: StringHelper::randomString());
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getPayload(): array
85
    {
86
        // Explicitly set ?
87
        if ($this->payload !== null) {
88
            return (array)$this->payload;
89
        }
90
91
        $payload = array_merge(
92
            $this->getObjectPayload(),
93
            [
94
                'timelineIFrame' => $this->timelineIFrame,
95
                'extraData' => $this->extraData
96
            ],
97
            $this->properties
98
        );
99
100
        return array_filter($payload);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    protected function getObjectPayload(): array
107
    {
108
        if(is_numeric($this->object)) {
109
            $this->object = ['objectId' => $this->object];
110
        }
111
112
        return (array)$this->object;
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    protected function prepare(array $criteria = [])
119
    {
120
        ObjectHelper::populate(
121
            $this,
122
            $criteria
123
        );
124
    }
125
126
    /**
127
     * @param array $config
128
     * @param null $source
129
     * @return mixed
130
     * @throws \yii\base\InvalidConfigException
131
     */
132
    public function upsert(array $config = [], $source = null)
133
    {
134
        $this->prepare($config);
135
136
        return HubSpot::getInstance()
137
            ->getResources()
138
            ->getTimelineEvents()
139
            ->upsertByCriteria($this, $source);
140
    }
141
142
    /**
143
     * @param array $config
144
     * @return mixed
145
     */
146
    public function upsertJob(array $config = [])
147
    {
148
        $this->prepare($config);
149
150
        return HubSpot::getInstance()
151
            ->getResources()
152
            ->getTimelineEvents()
153
            ->upsertJob(
154
                $this->getId(),
155
                $this->getTypeId(),
156
                $this->getPayload(),
157
                $this->getConnection()
158
            );
159
    }
160
161
    /**
162
     * @param array $config
163
     * @param null $source
164
     * @return mixed
165
     * @throws \yii\base\InvalidConfigException
166
     */
167
    public function read(array $config = [], $source = null)
168
    {
169
        $this->prepare($config);
170
171
        return HubSpot::getInstance()
172
            ->getResources()
173
            ->getTimelineEvents()
174
            ->readByCriteria($this, $source);
175
    }
176
}
177