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

TimelineEvents::upsertJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 4
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\services\resources;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\helpers\ArrayHelper;
14
use flipbox\hubspot\connections\IntegrationConnectionInterface;
15
use flipbox\hubspot\criteria\TimelineEventCriteria;
16
use flipbox\hubspot\criteria\TimelineEventCriteriaInterface;
17
use flipbox\hubspot\fields\Resources;
18
use flipbox\hubspot\helpers\TransformerHelper;
19
use flipbox\hubspot\HubSpot;
20
use flipbox\hubspot\pipeline\Resource;
21
use flipbox\hubspot\queue\jobs\UpsertTimelineEvent;
22
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
23
use Flipbox\Relay\HubSpot\Builder\Resources\TimelineEvent\Upsert;
24
use Flipbox\Relay\HubSpot\Builder\Resources\TimelineEvent\Read;
25
use League\Pipeline\PipelineBuilderInterface;
26
use Psr\SimpleCache\CacheInterface;
27
use yii\base\Component;
28
29
/**
30
 * @author Flipbox Factory <[email protected]>
31
 * @since 1.0.0
32
 */
33
class TimelineEvents extends Component
34
{
35
    /**
36
     * The HubSpot Resource name
37
     */
38
    const HUBSPOT_RESOURCE = 'timelineEvents';
39
40
    /*******************************************
41
     * ELEMENT SYNC JOBS
42
     *******************************************/
43
44
    /**
45
     * @param string $id
46
     * @param string $typeId
47
     * @param array $payload
48
     * @param IntegrationConnectionInterface $connection
49
     * @return ArrayHelper|string
50
     */
51
    public function upsertJob(
52
        string $id,
53
        string $typeId,
54
        array $payload,
55
        IntegrationConnectionInterface $connection
56
    ) {
57
        return Craft::$app->getQueue()->push(new UpsertTimelineEvent([
58
            'id' => $id,
59
            'typeId' => $typeId,
60
            'payload' => $payload,
61
            'connection' => $connection
62
        ]));
63
    }
64
65
    /*******************************************
66
     * GET / READ
67
     *******************************************/
68
69
    /**
70
     * @param string $id
71
     * @param string $typeId
72
     * @param IntegrationConnectionInterface $connection
73
     * @param CacheInterface $cache
74
     * @param TransformerCollectionInterface|null $transformer
75
     * @param mixed|null $source
76
     * @return mixed
77
     */
78
    public function read(
79
        string $id,
80
        string $typeId,
81
        IntegrationConnectionInterface $connection,
82
        CacheInterface $cache,
83
        TransformerCollectionInterface $transformer = null,
84
        $source = null
85
    ) {
86
        return $this->readPipeline(
87
            $id,
88
            $typeId,
89
            $connection,
90
            $cache,
91
            $transformer
92
        )($source);
93
    }
94
95
    /**
96
     * @param string $id
97
     * @param string $typeId
98
     * @param IntegrationConnectionInterface $connection
99
     * @param CacheInterface $cache
100
     * @param TransformerCollectionInterface|null $transformer
101
     * @return Resource
102
     */
103
    public function readPipeline(
104
        string $id,
105
        string $typeId,
106
        IntegrationConnectionInterface $connection,
107
        CacheInterface $cache,
108
        TransformerCollectionInterface $transformer = null
109
    ): PipelineBuilderInterface {
110
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
111
            'resource' => [Read::class]
112
        ]);
113
114
        $logger = HubSpot::getInstance()->getPsrLogger();
115
116
        return (new Resource(
117
            (new Read(
118
                $connection->getAppId(),
119
                $id,
120
                $typeId,
121
                $connection,
122
                $cache,
123
                $logger
124
            ))->build(),
125
            $transformer,
126
            $logger
127
        ));
128
    }
129
130
    /**
131
     * @param TimelineEventCriteriaInterface $criteria
132
     * @param null $source
133
     * @return mixed
134
     * @throws \yii\base\InvalidConfigException
135
     */
136
    public function readByCriteria(
137
        TimelineEventCriteriaInterface $criteria,
138
        $source = null
139
    ) {
140
        return $this->readPipelineByCriteria(
141
            $criteria
142
        )($source);
143
    }
144
145
    /**
146
     * @param TimelineEventCriteriaInterface $criteria
147
     * @return PipelineBuilderInterface
148
     * @throws \yii\base\InvalidConfigException
149
     */
150
    public function readPipelineByCriteria(
151
        TimelineEventCriteriaInterface $criteria
152
    ): PipelineBuilderInterface {
153
        return $this->readPipeline(
154
            $criteria->getId(),
155
            $criteria->getTypeId(),
156
            $criteria->getConnection(),
157
            $criteria->getCache(),
158
            $criteria->getTransformer()
159
        );
160
    }
161
162
    /*******************************************
163
     * UPSERT
164
     *******************************************/
165
166
    /**
167
     * @param string $id
168
     * @param string $typeId
169
     * @param array $payload
170
     * @param IntegrationConnectionInterface $connection
171
     * @param TransformerCollectionInterface|null $transformer
172
     * @param mixed|null $source
173
     * @return mixed
174
     */
175
    public function upsert(
176
        string $id,
177
        string $typeId,
178
        array $payload,
179
        IntegrationConnectionInterface $connection,
180
        TransformerCollectionInterface $transformer = null,
181
        $source = null
182
    ) {
183
        return $this->upsertPipeline(
184
            $id,
185
            $typeId,
186
            $payload,
187
            $connection,
188
            $transformer
189
        )($source);
190
    }
191
192
    /**
193
     * @param string $id
194
     * @param string $typeId
195
     * @param array $payload
196
     * @param IntegrationConnectionInterface $connection
197
     * @param TransformerCollectionInterface|null $transformer
198
     * @return Resource
199
     */
200
    public function upsertPipeline(
201
        string $id,
202
        string $typeId,
203
        array $payload,
204
        IntegrationConnectionInterface $connection,
205
        TransformerCollectionInterface $transformer = null
206
    ): PipelineBuilderInterface {
207
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
208
            'resource' => [Upsert::class]
209
        ]);
210
211
        $payload['id'] = $id;
212
        $payload['eventTypeId'] = $typeId;
213
214
        $logger = HubSpot::getInstance()->getPsrLogger();
215
216
        return (new Resource(
217
            (new Upsert(
218
                $connection->getAppId(),
219
                $payload,
220
                $connection,
221
                $logger
222
            ))->build(),
223
            $transformer,
224
            $logger
225
        ));
226
    }
227
228
    /**
229
     * @param TimelineEventCriteriaInterface $criteria
230
     * @param null $source
231
     * @throws \yii\base\InvalidConfigException
232
     * @return mixed
233
     */
234
    public function upsertByCriteria(
235
        TimelineEventCriteriaInterface $criteria,
236
        $source = null
237
    ) {
238
        return $this->upsertPipelineByCriteria(
239
            $criteria
240
        )($source);
241
    }
242
243
    /**
244
     * @param TimelineEventCriteriaInterface $criteria
245
     * @return PipelineBuilderInterface
246
     * @throws \yii\base\InvalidConfigException
247
     */
248
    public function upsertPipelineByCriteria(
249
        TimelineEventCriteriaInterface $criteria
250
    ): PipelineBuilderInterface {
251
        return $this->upsertPipeline(
252
            $criteria->getId(),
253
            $criteria->getTypeId(),
254
            $criteria->getPayload(),
255
            $criteria->getConnection(),
256
            $criteria->getTransformer()
257
        );
258
    }
259
}
260