Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

Companies   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 0
loc 258
ccs 0
cts 149
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getCriteria() 0 4 1
A get() 0 19 1
A getFromCriteria() 0 10 1
A create() 0 17 1
A createFromCriteria() 0 9 1
A update() 0 21 1
A updateFromCriteria() 0 11 1
A upsert() 0 23 2
A upsertFromCriteria() 0 11 1
A delete() 0 19 1
A deleteFromCriteria() 0 10 1
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\criteria\CompanyCriteria;
12
use flipbox\hubspot\criteria\ResourceCriteriaInterface;
13
use flipbox\hubspot\connections\ConnectionInterface;
14
use flipbox\hubspot\helpers\TransformerHelper;
15
use flipbox\hubspot\pipeline\Resource;
16
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
17
use League\Pipeline\PipelineBuilderInterface;
18
use Psr\SimpleCache\CacheInterface;
19
use yii\base\Component;
20
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Read;
21
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Create;
22
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Delete;
23
use Flipbox\Relay\HubSpot\Builder\Resources\Company\Update;
24
25
26
/**
27
 * @author Flipbox Factory <[email protected]>
28
 * @since 1.0.0
29
 */
30
class Companies extends Component
31
{
32
    use traits\ElementCriteriaTrait,
33
        traits\TransformElementIdTrait,
34
        traits\SyncFromElementTrait,
35
        traits\DeleteFromElementTrait;
36
37
    /**
38
     * The HubSpot Resource name
39
     */
40
    const HUBSPOT_RESOURCE = 'companies';
41
42
    /**
43
     * @inheritdoc
44
     * @return CompanyCriteria
45
     */
46
    public function getCriteria(array $criteria = []): ResourceCriteriaInterface
47
    {
48
        return new CompanyCriteria($criteria);
49
    }
50
51
    /*******************************************
52
     * GET
53
     *******************************************/
54
55
    /**
56
     * @param string $id
57
     * @param ConnectionInterface $connection
58
     * @param CacheInterface $cache
59
     * @param TransformerCollectionInterface|null $transformer
60
     * @return Resource
61
     */
62
    public function get(
63
        $id,
64
        ConnectionInterface $connection,
65
        CacheInterface $cache,
66
        TransformerCollectionInterface $transformer = null
67
    ): PipelineBuilderInterface {
68
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
69
            'resource' => [Read::class]
70
        ]);
71
72
        return (new Resource(
73
            (new Read(
74
                $id,
75
                $connection,
76
                $cache
77
            ))->build(),
78
            $transformer
79
        ));
80
    }
81
82
    /**
83
     * @inheritdoc
84
     * @return Resource
85
     */
86
    public function getFromCriteria(
87
        ResourceCriteriaInterface $criteria
88
    ): PipelineBuilderInterface {
89
        return $this->get(
90
            $criteria->getId(),
91
            $criteria->getConnection(),
92
            $criteria->getCache(),
93
            $criteria->getTransformer()
94
        );
95
    }
96
97
    /*******************************************
98
     * CREATE
99
     *******************************************/
100
101
    /**
102
     * @param array $payload
103
     * @param ConnectionInterface $connection
104
     * @param TransformerCollectionInterface|null $transformer
105
     * @return Resource
106
     */
107
    public function create(
108
        array $payload,
109
        ConnectionInterface $connection,
110
        TransformerCollectionInterface $transformer = null
111
    ): PipelineBuilderInterface {
112
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
113
            'resource' => [Create::class]
114
        ]);
115
116
        return (new Resource(
117
            (new Create(
118
                $payload,
119
                $connection
120
            ))->build(),
121
            $transformer
122
        ));
123
    }
124
125
    /**
126
     * @param ResourceCriteriaInterface $criteria
127
     * @return Resource
128
     */
129
    public function createFromCriteria(
130
        ResourceCriteriaInterface $criteria
131
    ): PipelineBuilderInterface {
132
        return $this->create(
133
            $criteria->getPayload(),
134
            $criteria->getConnection(),
135
            $criteria->getTransformer()
136
        );
137
    }
138
139
    /*******************************************
140
     * UPDATE
141
     *******************************************/
142
143
    /**
144
     * @param string $id
145
     * @param array $payload
146
     * @param ConnectionInterface $connection
147
     * @param CacheInterface $cache
148
     * @param TransformerCollectionInterface|null $transformer
149
     * @return Resource
150
     */
151
    public function update(
152
        string $id,
153
        array $payload,
154
        ConnectionInterface $connection,
155
        CacheInterface $cache,
156
        TransformerCollectionInterface $transformer = null
157
    ): PipelineBuilderInterface {
158
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
159
            'resource' => [Update::class]
160
        ]);
161
162
        return (new Resource(
163
            (new Update(
164
                $id,
165
                $payload,
166
                $connection,
167
                $cache
168
            ))->build(),
169
            $transformer
170
        ));
171
    }
172
173
    /**
174
     * @param ResourceCriteriaInterface $criteria
175
     * @return Resource
176
     */
177
    public function updateFromCriteria(
178
        ResourceCriteriaInterface $criteria
179
    ): PipelineBuilderInterface {
180
        return $this->update(
181
            $criteria->getId(),
182
            $criteria->getPayload(),
183
            $criteria->getConnection(),
184
            $criteria->getCache(),
185
            $criteria->getTransformer()
186
        );
187
    }
188
189
    /*******************************************
190
     * UPSERT
191
     *******************************************/
192
193
    /**
194
     * @param array $payload
195
     * @param ConnectionInterface $connection
196
     * @param CacheInterface $cache
197
     * @param string|null $identifier
198
     * @param TransformerCollectionInterface|null $transformer
199
     * @return Resource
200
     */
201
    public function upsert(
202
        array $payload,
203
        ConnectionInterface $connection,
204
        CacheInterface $cache,
205
        string $identifier = null,
206
        TransformerCollectionInterface $transformer = null
207
    ): PipelineBuilderInterface {
208
        if (empty($identifier)) {
209
            return $this->create(
210
                $payload,
211
                $connection,
212
                $transformer
213
            );
214
        }
215
216
        return $this->update(
217
            $identifier,
218
            $payload,
219
            $connection,
220
            $cache,
221
            $transformer
222
        );
223
    }
224
225
    /**
226
     * @inheritdoc
227
     * @return Resource
228
     */
229
    public function upsertFromCriteria(
230
        ResourceCriteriaInterface $criteria
231
    ): PipelineBuilderInterface {
232
        return $this->upsert(
233
            $criteria->getPayload(),
234
            $criteria->getConnection(),
235
            $criteria->getCache(),
236
            $criteria->getId(),
237
            $criteria->getTransformer()
238
        );
239
    }
240
241
242
    /*******************************************
243
     * DELETE
244
     *******************************************/
245
246
    /**
247
     * @param string $id
248
     * @param ConnectionInterface $connection
249
     * @param CacheInterface $cache
250
     * @param TransformerCollectionInterface|null $transformer
251
     * @return Resource
252
     */
253
    public function delete(
254
        string $id,
255
        ConnectionInterface $connection,
256
        CacheInterface $cache,
257
        TransformerCollectionInterface $transformer = null
258
    ): PipelineBuilderInterface {
259
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
260
            'resource' => [Delete::class]
261
        ]);
262
263
        return (new Resource(
264
            (new Delete(
265
                $id,
266
                $connection,
267
                $cache
268
            ))->build(),
269
            $transformer
270
        ));
271
    }
272
273
    /**
274
     * @param ResourceCriteriaInterface $criteria
275
     * @return Resource
276
     */
277
    public function deleteFromCriteria(
278
        ResourceCriteriaInterface $criteria
279
    ): PipelineBuilderInterface {
280
        return $this->delete(
281
            $criteria->getId(),
282
            $criteria->getConnection(),
283
            $criteria->getCache(),
284
            $criteria->getTransformer()
285
        );
286
    }
287
}
288