Completed
Push — master ( 6e9196...c55090 )
by Nate
02:30
created

TimelineEventMutator::upsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/hubspot/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/hubspot
7
 */
8
9
namespace Flipbox\HubSpot\Criteria;
10
11
use Flipbox\HubSpot\Resources\TimelineEvent;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 */
18
class TimelineEventMutator extends AbstractCriteria
19
{
20
    use IntegrationConnectionTrait,
21
        CacheTrait;
22
23
    /**
24
     * @var string
25
     */
26
    public $id;
27
28
    /**
29
     * @var string|array
30
     */
31
    public $object;
32
33
    /**
34
     * The event type Id
35
     *
36
     * @var string
37
     */
38
    public $typeId;
39
40
    /**
41
     * @var array
42
     */
43
    public $extraData = [];
44
45
    /**
46
     * @var array|null
47
     */
48
    public $payload;
49
50
    /**
51
     * @var array
52
     */
53
    public $properties = [];
54
55
    /**
56
     * @var array
57
     */
58
    public $timelineIFrame = [];
59
60
    /**
61
     * @return string
62
     */
63
    public function getTypeId(): string
64
    {
65
        return $this->typeId;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getId(): string
72
    {
73
        return (string)($this->id ?: substr(str_shuffle(md5(time())),0, 36));
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getPayload(): array
80
    {
81
        // Explicitly set ?
82
        if ($this->payload !== null) {
83
            return (array)$this->payload;
84
        }
85
86
        $payload = array_merge(
87
            $this->getObjectPayload(),
88
            [
89
                'timelineIFrame' => $this->timelineIFrame,
90
                'extraData' => $this->extraData
91
            ],
92
            $this->properties
93
        );
94
95
        return array_filter($payload);
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    protected function getObjectPayload(): array
102
    {
103
        if (is_numeric($this->object)) {
104
            $this->object = ['objectId' => $this->object];
105
        }
106
107
        return (array)$this->object;
108
    }
109
110
    /**
111
     * @param array $criteria
112
     * @param array $config
113
     * @return ResponseInterface
114
     */
115
    public function upsert(array $criteria = [], array $config = []): ResponseInterface
116
    {
117
        $this->populate($criteria);
118
119
        return TimelineEvent::upsert(
120
            $this->getId(),
121
            $this->getTypeId(),
122
            $this->getPayload(),
123
            $this->getConnection(),
124
            $this->getCache(),
125
            $this->getLogger(),
126
            $config
127
        );
128
    }
129
}
130