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

Contacts::rawUpdatePipeline()   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 flipbox\hubspot\builders\ContactBuilder;
12
use flipbox\hubspot\builders\ObjectBuilderInterface;
13
use flipbox\hubspot\connections\ConnectionInterface;
14
use flipbox\hubspot\criteria\ContactCriteria;
15
use flipbox\hubspot\criteria\ObjectCriteriaInterface;
16
use flipbox\hubspot\helpers\TransformerHelper;
17
use flipbox\hubspot\HubSpot;
18
use flipbox\hubspot\pipeline\Resource;
19
use flipbox\hubspot\traits\CacheResolverTrait;
20
use flipbox\hubspot\traits\ConnectionResolverTrait;
21
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
22
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Create;
23
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Delete;
24
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\ReadById;
25
use Flipbox\Relay\HubSpot\Builder\Resources\Contact\Update;
26
use League\Pipeline\PipelineBuilderInterface;
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 Contacts extends Component
35
{
36
    use ConnectionResolverTrait,
37
        CacheResolverTrait,
38
        traits\SyncByElementTrait,
39
        traits\ReadObjectTrait,
40
        traits\UpsertObjectTrait,
41
        traits\DeleteObjectTrait;
42
43
    /**
44
     * The HubSpot Resource name
45
     */
46
    const HUBSPOT_RESOURCE = 'contacts';
47
48
    /**
49
     * @param array $config
50
     * @return ObjectCriteriaInterface
51
     */
52
    public function getCriteria(array $config = []): ObjectCriteriaInterface
53
    {
54
        return new ContactCriteria($config);
55
    }
56
57
    /**
58
     * @param array $config
59
     * @return ObjectBuilderInterface
60
     */
61
    public function getBuilder(array $config = []): ObjectBuilderInterface
62
    {
63
        return new ContactBuilder($config);
64
    }
65
66
    /*******************************************
67
     * ELEMENT SYNC JOBS
68
     *******************************************/
69
70
//    /**
71
//     * @param ElementInterface $element
72
//     * @param Objects $field
73
//     * @return null|string
74
//     */
75
//    public function createElementSyncToJob(
76
//        ElementInterface $element,
77
//        Objects $field
78
//    ) {
79
//        return Craft::$app->getQueue()->push(new SyncElementTo([
80
//            'element' => $element,
81
//            'field' => $field,
82
//            'resource' => self::HUBSPOT_RESOURCE
83
//        ]));
84
//    }
85
//
86
//    /**
87
//     * @param ElementInterface $element
88
//     * @param Objects $field
89
//     * @return null|string
90
//     */
91
//    public function createElementSyncFromJob(
92
//        ElementInterface $element,
93
//        Objects $field
94
//    ) {
95
//        return Craft::$app->getQueue()->push(new SyncElementFrom([
96
//            'element' => $element,
97
//            'field' => $field,
98
//            'resource' => self::HUBSPOT_RESOURCE
99
//        ]));
100
//    }
101
102
    /*******************************************
103
     * READ
104
     *******************************************/
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function rawReadPipeline(
110
        string $id,
111
        ConnectionInterface $connection = null,
112
        CacheInterface $cache = null,
113
        TransformerCollectionInterface $transformer = null
114
    ): PipelineBuilderInterface {
115
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
116
            'resource' => [ReadById::class]
117
        ]);
118
119
        return (new Resource(
120
            $this->rawHttpReadRelay(
121
                $id,
122
                $connection,
123
                $cache
124
            ),
125
            $transformer,
126
            HubSpot::getInstance()->getPsrLogger()
127
        ));
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function rawHttpReadRelay(
134
        string $id,
135
        ConnectionInterface $connection = null,
136
        CacheInterface $cache = null
137
    ): callable {
138
        return (new ReadById(
139
            $id,
140
            $this->resolveConnection($connection),
141
            $this->resolveCache($cache),
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 136 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...
142
            HubSpot::getInstance()->getPsrLogger()
143
        ))->build();
144
    }
145
146
147
    /*******************************************
148
     * CREATE
149
     *******************************************/
150
151
    /**
152
     * @inheritdoc
153
     */
154
    public function rawCreatePipeline(
155
        array $payload,
156
        ConnectionInterface $connection = null,
157
        TransformerCollectionInterface $transformer = null
158
    ): PipelineBuilderInterface {
159
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
160
            'resource' => [Create::class]
161
        ]);
162
163
        return (new Resource(
164
            $this->rawHttpCreateRelay(
165
                $payload,
166
                $connection
167
            ),
168
            $transformer,
169
            HubSpot::getInstance()->getPsrLogger()
170
        ));
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function rawHttpCreateRelay(
177
        array $payload,
178
        ConnectionInterface $connection = null
179
    ): callable {
180
        return (new Create(
181
            $payload,
182
            $this->resolveConnection($connection),
183
            HubSpot::getInstance()->getPsrLogger()
184
        ))->build();
185
    }
186
187
    /*******************************************
188
     * UPDATE
189
     *******************************************/
190
191
    /**
192
     * @inheritdoc
193
     */
194
    public function rawUpdatePipeline(
195
        string $id,
196
        array $payload,
197
        ConnectionInterface $connection = null,
198
        CacheInterface $cache = null,
199
        TransformerCollectionInterface $transformer = null
200
    ): PipelineBuilderInterface {
201
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
202
            'resource' => [Update::class]
203
        ]);
204
205
        return (new Resource(
206
            $this->rawHttpUpdateRelay(
207
                $id,
208
                $payload,
209
                $connection,
210
                $cache
211
            ),
212
            $transformer,
213
            HubSpot::getInstance()->getPsrLogger()
214
        ));
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220
    public function rawHttpUpdateRelay(
221
        string $id,
222
        array $payload,
223
        ConnectionInterface $connection = null,
224
        CacheInterface $cache = null
225
    ): callable {
226
        return (new Update(
227
            $id,
228
            $payload,
229
            $this->resolveConnection($connection),
230
            $this->resolveCache($cache),
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 224 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...
231
            HubSpot::getInstance()->getPsrLogger()
232
        ))->build();
233
    }
234
235
    /*******************************************
236
     * DELETE
237
     *******************************************/
238
239
    /**
240
     * @inheritdoc
241
     */
242
    public function rawDeletePipeline(
243
        string $id,
244
        ConnectionInterface $connection = null,
245
        CacheInterface $cache = null,
246
        TransformerCollectionInterface $transformer = null
247
    ): PipelineBuilderInterface {
248
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
249
            'resource' => [Delete::class]
250
        ]);
251
252
        return (new Resource(
253
            $this->rawHttpDeleteRelay(
254
                $id,
255
                $connection,
256
                $cache
257
            ),
258
            $transformer,
259
            HubSpot::getInstance()->getPsrLogger()
260
        ));
261
    }
262
263
    /**
264
     * @inheritdoc
265
     */
266
    public function rawHttpDeleteRelay(
267
        string $id,
268
        ConnectionInterface $connection = null,
269
        CacheInterface $cache = null
270
    ): callable {
271
        return (new Delete(
272
            $id,
273
            $this->resolveConnection($connection),
274
            $this->resolveCache($cache),
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 269 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...
275
            HubSpot::getInstance()->getPsrLogger()
276
        ))->build();
277
    }
278
}
279