Completed
Push — develop ( 639fa1...b48ec5 )
by Nate
02:32
created

TimelineEvents::rawReadPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
255
256
        return (new Read(
257
            $connection->getAppId(),
258
            $id,
259
            $typeId,
260
            $connection,
261
            $this->resolveCache($cache),
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 252 can be null; however, flipbox\hubspot\traits\C...erTrait::resolveCache() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
262
            HubSpot::getInstance()->getPsrLogger()
263
        ))->build();
264
    }
265
266
267
    /*******************************************
268
     * UPSERT
269
     *******************************************/
270
271
    /**
272
     * @param TimelineEventBuilderInterface $builder
273
     * @param IntegrationConnectionInterface $connection
274
     * @param CacheInterface $cache
275
     * @param TransformerCollectionInterface|null $transformer
276
     * @param mixed|null $source
277
     * @return mixed
278
     * @throws \yii\base\InvalidConfigException
279
     */
280
    public function upsert(
281
        TimelineEventBuilderInterface $builder,
282
        IntegrationConnectionInterface $connection = null,
283
        CacheInterface $cache = null,
284
        TransformerCollectionInterface $transformer = null,
285
        $source = null
286
    ) {
287
        return $this->rawUpsert(
288
            $builder->getTypeId(),
289
            $builder->getId(),
290
            $builder->getPayload(),
291
            $connection,
292
            $cache,
293
            $transformer,
294
            $source
295
        );
296
    }
297
298
    /**
299
     * @param string $typeId
300
     * @param string $id
301
     * @param array $payload
302
     * @param IntegrationConnectionInterface|null $connection
303
     * @param CacheInterface|null $cache
304
     * @param TransformerCollectionInterface|null $transformer
305
     * @param null $source
306
     * @return mixed
307
     * @throws \yii\base\InvalidConfigException
308
     */
309
    public function rawUpsert(
310
        string $typeId,
311
        string $id,
312
        array $payload,
313
        IntegrationConnectionInterface $connection = null,
314
        CacheInterface $cache = null,
315
        TransformerCollectionInterface $transformer = null,
316
        $source = null
317
    ) {
318
        return $this->rawUpsertPipeline(
319
            $typeId,
320
            $id,
321
            $payload,
322
            $connection,
323
            $cache,
324
            $transformer
325
        )($source);
326
    }
327
328
    /**
329
     * @param TimelineEventBuilderInterface $builder
330
     * @param IntegrationConnectionInterface|null $connection
331
     * @param CacheInterface|null $cache
332
     * @param TransformerCollectionInterface|null $transformer
333
     * @return PipelineBuilderInterface
334
     * @throws \yii\base\InvalidConfigException
335
     */
336
    public function upsertPipeline(
337
        TimelineEventBuilderInterface $builder,
338
        IntegrationConnectionInterface $connection = null,
339
        CacheInterface $cache = null,
340
        TransformerCollectionInterface $transformer = null
341
    ): PipelineBuilderInterface {
342
        return $this->rawUpsertPipeline(
343
            $builder->getTypeId(),
344
            $builder->getId(),
345
            $builder->getPayload(),
346
            $connection,
347
            $cache,
348
            $transformer
349
        );
350
    }
351
352
    /**
353
     * @param string $typeId
354
     * @param string $id
355
     * @param array $payload
356
     * @param IntegrationConnectionInterface|null $connection
357
     * @param CacheInterface|null $cache
358
     * @param TransformerCollectionInterface|null $transformer
359
     * @return PipelineBuilderInterface
360
     * @throws \yii\base\InvalidConfigException
361
     */
362
    public function rawUpsertPipeline(
363
        string $typeId,
364
        string $id,
365
        array $payload,
366
        IntegrationConnectionInterface $connection = null,
367
        CacheInterface $cache = null,
368
        TransformerCollectionInterface $transformer = null
369
    ): PipelineBuilderInterface {
370
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
371
            'resource' => [Upsert::class]
372
        ]);
373
374
        return (new Resource(
375
            $this->rawHttpUpsertRelay(
376
                $typeId,
377
                $id,
378
                $payload,
379
                $connection,
380
                $cache
381
            ),
382
            $transformer,
383
            HubSpot::getInstance()->getPsrLogger()
384
        ));
385
    }
386
387
    /**
388
     * @param TimelineEventBuilderInterface $builder
389
     * @param IntegrationConnectionInterface|null $connection
390
     * @param CacheInterface|null $cache
391
     * @return ResponseInterface
392
     * @throws \yii\base\InvalidConfigException
393
     */
394
    public function httpUpsert(
395
        TimelineEventBuilderInterface $builder,
396
        IntegrationConnectionInterface $connection = null,
397
        CacheInterface $cache = null
398
    ): ResponseInterface {
399
        return $this->rawHttpUpsert(
400
            $builder->getTypeId(),
401
            $builder->getId(),
402
            $builder->getPayload(),
403
            $connection,
404
            $cache
405
        );
406
    }
407
408
    /**
409
     * @param string $typeId
410
     * @param string $id
411
     * @param array $payload
412
     * @param IntegrationConnectionInterface|null $connection
413
     * @param CacheInterface|null $cache
414
     * @return ResponseInterface
415
     * @throws \yii\base\InvalidConfigException
416
     */
417
    public function rawHttpUpsert(
418
        string $typeId,
419
        string $id,
420
        array $payload,
421
        IntegrationConnectionInterface $connection = null,
422
        CacheInterface $cache = null
423
    ): ResponseInterface {
424
        return $this->rawHttpUpsertRelay(
425
            $typeId,
426
            $id,
427
            $payload,
428
            $connection,
429
            $cache
430
        )();
431
    }
432
433
    /**
434
     * @param TimelineEventBuilderInterface $builder
435
     * @param IntegrationConnectionInterface|null $connection
436
     * @param CacheInterface|null $cache
437
     * @return callable
438
     * @throws \yii\base\InvalidConfigException
439
     */
440
    public function httpUpsertRelay(
441
        TimelineEventBuilderInterface $builder,
442
        IntegrationConnectionInterface $connection = null,
443
        CacheInterface $cache = null
444
    ): callable {
445
        return $this->rawHttpUpsertRelay(
446
            $builder->getTypeId(),
447
            $builder->getId(),
448
            $builder->getPayload(),
449
            $connection,
450
            $cache
451
        );
452
    }
453
454
    /**
455
     * @param string $typeId
456
     * @param string $id
457
     * @param array $payload
458
     * @param IntegrationConnectionInterface|null $connection
459
     * @param CacheInterface|null $cache
460
     * @return callable
461
     * @throws \yii\base\InvalidConfigException
462
     */
463
    public function rawHttpUpsertRelay(
464
        string $typeId,
465
        string $id,
466
        array $payload,
467
        IntegrationConnectionInterface $connection = null,
468
        CacheInterface $cache = null
469
    ): callable {
470
        $connection = $this->resolveConnection($connection);
0 ignored issues
show
Bug introduced by
It seems like $connection can be null; however, resolveConnection() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
471
472
        return (new Upsert(
473
            $connection->getAppId(),
474
            $typeId,
475
            $id,
476
            $payload,
477
            $connection,
478
            $this->resolveCache($cache),
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 468 can be null; however, flipbox\hubspot\traits\C...erTrait::resolveCache() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
479
            HubSpot::getInstance()->getPsrLogger()
480
        ))->build();
481
    }
482
}
483