Completed
Push — develop ( e7d2ea...3644a8 )
by Nate
06:56
created

TimelineEvents::readByCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
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://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\connections\IntegrationConnectionInterface;
12
use flipbox\hubspot\criteria\TimelineEventCriteria;
13
use flipbox\hubspot\helpers\TransformerHelper;
14
use flipbox\hubspot\HubSpot;
15
use flipbox\hubspot\pipeline\Resource;
16
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
17
use Flipbox\Relay\HubSpot\Builder\Resources\TimelineEvent\Upsert;
18
use Flipbox\Relay\HubSpot\Builder\Resources\TimelineEvent\Read;
19
use League\Pipeline\PipelineBuilderInterface;
20
use Psr\SimpleCache\CacheInterface;
21
use yii\base\Component;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class TimelineEvents extends Component
28
{
29
    /**
30
     * The HubSpot Resource name
31
     */
32
    const HUBSPOT_RESOURCE = 'timelineEvents';
33
34
    /*******************************************
35
     * GET / READ
36
     *******************************************/
37
38
    /**
39
     * @param string $id
40
     * @param string $typeId
41
     * @param IntegrationConnectionInterface $connection
42
     * @param CacheInterface $cache
43
     * @param TransformerCollectionInterface|null $transformer
44
     * @param mixed|null $source
45
     * @return mixed
46
     */
47
    public function read(
48
        string $id,
49
        string $typeId,
50
        IntegrationConnectionInterface $connection,
51
        CacheInterface $cache,
52
        TransformerCollectionInterface $transformer = null,
53
        $source = null
54
    ) {
55
        return $this->readPipeline(
56
            $id,
57
            $typeId,
58
            $connection,
59
            $cache,
60
            $transformer
61
        )($source);
62
    }
63
64
    /**
65
     * @param string $id
66
     * @param string $typeId
67
     * @param IntegrationConnectionInterface $connection
68
     * @param CacheInterface $cache
69
     * @param TransformerCollectionInterface|null $transformer
70
     * @return Resource
71
     */
72
    public function readPipeline(
73
        string $id,
74
        string $typeId,
75
        IntegrationConnectionInterface $connection,
76
        CacheInterface $cache,
77
        TransformerCollectionInterface $transformer = null
78
    ): PipelineBuilderInterface {
79
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
80
            'resource' => [Read::class]
81
        ]);
82
83
        $logger = HubSpot::getInstance()->getPsrLogger();
84
85
        return (new Resource(
86
            (new Read(
87
                $connection->getAppId(),
88
                $id,
89
                $typeId,
90
                $connection,
91
                $cache,
92
                $logger
93
            ))->build(),
94
            $transformer,
95
            $logger
96
        ));
97
    }
98
99
    /**
100
     * @param TimelineEventCriteria $criteria
101
     * @param null $source
102
     * @return mixed
103
     * @throws \yii\base\InvalidConfigException
104
     */
105
    public function readByCriteria(
106
        TimelineEventCriteria $criteria,
107
        $source = null
108
    ) {
109
        return $this->readPipelineByCriteria(
110
            $criteria
111
        )($source);
112
    }
113
114
    /**
115
     * @param TimelineEventCriteria $criteria
116
     * @return PipelineBuilderInterface
117
     * @throws \yii\base\InvalidConfigException
118
     */
119
    public function readPipelineByCriteria(
120
        TimelineEventCriteria $criteria
121
    ): PipelineBuilderInterface {
122
        return $this->readPipeline(
123
            $criteria->getId(),
124
            $criteria->getTypeId(),
125
            $criteria->getConnection(),
126
            $criteria->getCache(),
127
            $criteria->getTransformer()
0 ignored issues
show
Bug introduced by
It seems like $criteria->getTransformer() targeting flipbox\hubspot\criteria...Trait::getTransformer() can also be of type object or string; however, flipbox\hubspot\services...eEvents::readPipeline() does only seem to accept null|object<flipbox\hubs...merCollectionInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
128
        );
129
    }
130
131
    /*******************************************
132
     * UPSERT
133
     *******************************************/
134
135
    /**
136
     * @param string $id
137
     * @param string $typeId
138
     * @param array $payload
139
     * @param IntegrationConnectionInterface $connection
140
     * @param TransformerCollectionInterface|null $transformer
141
     * @param mixed|null $source
142
     * @return mixed
143
     */
144
    public function upsert(
145
        string $id,
146
        string $typeId,
147
        array $payload,
148
        IntegrationConnectionInterface $connection,
149
        TransformerCollectionInterface $transformer = null,
150
        $source = null
151
    ) {
152
        return $this->upsertPipeline(
153
            $id,
154
            $typeId,
155
            $payload,
156
            $connection,
157
            $transformer
158
        )($source);
159
    }
160
161
    /**
162
     * @param string $id
163
     * @param string $typeId
164
     * @param array $payload
165
     * @param IntegrationConnectionInterface $connection
166
     * @param TransformerCollectionInterface|null $transformer
167
     * @return Resource
168
     */
169
    public function upsertPipeline(
170
        string $id,
171
        string $typeId,
172
        array $payload,
173
        IntegrationConnectionInterface $connection,
174
        TransformerCollectionInterface $transformer = null
175
    ): PipelineBuilderInterface {
176
        $transformer = TransformerHelper::populateTransformerCollection($transformer, [
177
            'resource' => [Upsert::class]
178
        ]);
179
180
        $payload['id'] = $id;
181
        $payload['eventTypeId'] = $typeId;
182
183
        $logger = HubSpot::getInstance()->getPsrLogger();
184
185
        return (new Resource(
186
            (new Upsert(
187
                $connection->getAppId(),
188
                $payload,
189
                $connection,
190
                $logger
191
            ))->build(),
192
            $transformer,
193
            $logger
194
        ));
195
    }
196
197
    /**
198
     * @param TimelineEventCriteria $criteria
199
     * @param null $source
200
     * @throws \yii\base\InvalidConfigException
201
     * @return mixed
202
     */
203
    public function upsertByCriteria(
204
        TimelineEventCriteria $criteria,
205
        $source = null
206
    ) {
207
        return $this->upsertPipelineByCriteria(
208
            $criteria
209
        )($source);
210
    }
211
212
    /**
213
     * @param TimelineEventCriteria $criteria
214
     * @return PipelineBuilderInterface
215
     * @throws \yii\base\InvalidConfigException
216
     */
217
    public function upsertPipelineByCriteria(
218
        TimelineEventCriteria $criteria
219
    ): PipelineBuilderInterface {
220
        return $this->upsertPipeline(
221
            $criteria->getId(),
222
            $criteria->getTypeId(),
223
            $criteria->getPayload(),
224
            $criteria->getConnection(),
225
            $criteria->getTransformer()
0 ignored issues
show
Bug introduced by
It seems like $criteria->getTransformer() targeting flipbox\hubspot\criteria...Trait::getTransformer() can also be of type object or string; however, flipbox\hubspot\services...vents::upsertPipeline() does only seem to accept null|object<flipbox\hubs...merCollectionInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
226
        );
227
    }
228
}
229