Completed
Push — develop ( 0ca1ab...29c245 )
by Nate
03:14
created

TimelineEventCriteria::getPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.6666
cc 2
nc 2
nop 0
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\connections\IntegrationConnectionInterface;
14
use flipbox\hubspot\HubSpot;
15
use flipbox\hubspot\services\Connections;
16
use yii\base\BaseObject;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class TimelineEventCriteria extends BaseObject
23
{
24
    use traits\TransformerCollectionTrait,
25
        traits\IntegrationConnectionTrait;
26
27
    /**
28
     * The event Id
29
     * @var string
30
     */
31
    public $id;
32
33
    /**
34
     * @var mixed
35
     */
36
    public $contact;
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->getContactPayload(),
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 getContactPayload(): array
107
    {
108
        return ['objectId' => 101];
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    protected function prepare(array $criteria = [])
115
    {
116
        ObjectHelper::populate(
117
            $this,
118
            $criteria
119
        );
120
    }
121
122
    /**
123
     * @param array $config
124
     * @param null $source
125
     * @return mixed
126
     */
127
    public function upsert(array $config = [], $source = null)
128
    {
129
        $this->prepare($config);
130
131
        return HubSpot::getInstance()
132
            ->getResources()
133
            ->getTimelineEvents()
134
            ->upsertByCriteria($this, $source);
135
    }
136
137
}