Completed
Pull Request — master (#62)
by
unknown
02:16
created

Timeline::createOrUpdate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 8
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace SevenShores\Hubspot\Resources;
4
5
use DateTime;
6
7
class Timeline extends Resource
8
{
9
    /**
10
     * Create or Update Timeline Event
11
     *
12
     * @param int           $appId
13
     * @param int           $eventTypeId
14
     * @param string        $id
15
     * @param int|null      $objectId
16
     * @param string|null   $email
17
     * @param string|null   $utk
18
     * @param array         $extraData
19
     * @param DateTime|null $timestamp
20
     *
21
     * @return mixed
22
     *
23
     * @see http://developers.hubspot.com/docs/methods/timeline/create-or-update-event
24
     */
25
    public function createOrUpdate(
26
        $appId,
27
        $eventTypeId,
28
        $id,
29
        $objectId = null,
30
        $email = null,
31
        $utk = null,
32
        $extraData = [],
33
        DateTime $timestamp = null
34
    ) {
35
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event";
36
37
        $data['json'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
38
            'eventTypeId' => $eventTypeId,
39
            'id'          => $id,
40
            'objectId'    => $objectId,
41
            'email'       => $email,
42
            'utk'         => $utk,
43
            'extraData'   => $extraData,
44
        ];
45
46
        if (isset($timestamp)) {
47
            // Times must be in milliseconds epoch time.
48
            $data['json']['timestamp'] = $timestamp->getTimestamp() * 1000;
49
        }
50
51
        return $this->client->request('put', $endpoint, $data);
52
    }
53
54
    /**
55
     * Get Timeline Event Types
56
     *
57
     * @param int $appId
58
     *
59
     * @return mixed
60
     *
61
     * @see http://developers.hubspot.com/docs/methods/timeline/get-event-types
62
     */
63
    public function getEventTypes($appId)
64
    {
65
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types";
66
        return $this->client->request('get', $endpoint);
67
    }
68
69
    /**
70
     * Create Timeline Event Type
71
     *
72
     * @param int         $appId
73
     * @param string      $name
74
     * @param string|null $headerTemplate
75
     * @param string|null $detailTemplate
76
     * @param string|null $objectType
77
     *
78
     * @return mixed
79
     *
80
     * @see http://developers.hubspot.com/docs/methods/timeline/create-event-type
81
     */
82 View Code Duplication
    public function createEventType(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
        $appId,
84
        $name,
85
        $headerTemplate = null,
86
        $detailTemplate = null,
87
        $objectType = null
88
    ) {
89
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types";
90
91
        $data['json'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
92
            'applicationId'  => $appId,
93
            'name'           => $name,
94
            'headerTemplate' => $headerTemplate,
95
            'detailTemplate' => $detailTemplate,
96
            'objectType'     => $objectType,
97
        ];
98
99
        return $this->client->request('post', $endpoint, $data);
100
    }
101
102
    /**
103
     * Update Timeline Event Type
104
     *
105
     * @param int         $appId
106
     * @param int         $eventTypeId
107
     * @param string|null $name
108
     * @param string|null $headerTemplate
109
     * @param string|null $detailTemplate
110
     * @param string|null $objectType
111
     *
112
     * @return mixed
113
     *
114
     * @see http://developers.hubspot.com/docs/methods/timeline/update-event-type
115
     */
116 View Code Duplication
    public function updateEventType(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
        $appId,
118
        $eventTypeId,
119
        $name = null,
120
        $headerTemplate = null,
121
        $detailTemplate = null,
122
        $objectType = null
123
    ) {
124
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types/{$eventTypeId}";
125
126
        $data['json'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
127
            'applicationId'  => $appId,
128
            'name'           => $name,
129
            'headerTemplate' => $headerTemplate,
130
            'detailTemplate' => $detailTemplate,
131
            'objectType'     => $objectType,
132
        ];
133
134
        return $this->client->request('put', $endpoint, $data);
135
    }
136
137
    /**
138
     * Delete Timeline Event Type
139
     *
140
     * @param int $appId
141
     * @param int $eventTypeId
142
     *
143
     * @return mixed
144
     *
145
     * @see http://developers.hubspot.com/docs/methods/timeline/delete-event-type
146
     */
147
    public function deleteEventType($appId, $eventTypeId)
148
    {
149
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types/{$eventTypeId}";
150
        return $this->client->request('delete', $endpoint);
151
    }
152
153
    /**
154
     * Get Properties for Timeline Event Type
155
     *
156
     * @param int $appId
157
     * @param int $eventTypeId
158
     *
159
     * @return mixed
160
     *
161
     * @see http://developers.hubspot.com/docs/methods/timeline/get-timeline-event-type-properties
162
     */
163
    public function getEventTypeProperties($appId, $eventTypeId)
164
    {
165
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types/{$eventTypeId}/properties";
166
        return $this->client->request('get', $endpoint);
167
    }
168
169
    /**
170
     * Create Property for Timeline Event Type
171
     *
172
     * @param int         $appId
173
     * @param int         $eventTypeId
174
     * @param string      $name
175
     * @param string      $label
176
     * @param string      $propertyType
177
     * @param string|null $objectProperty
178
     * @param array       $options
179
     *
180
     * @return mixed
181
     *
182
     * @see http://developers.hubspot.com/docs/methods/timeline/create-timeline-event-type-property
183
     */
184
    public function createEventTypeProperty(
185
        $appId,
186
        $eventTypeId,
187
        $name,
188
        $label,
189
        $propertyType,
190
        $objectProperty = null,
191
        $options = []
192
    ) {
193
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types/{$eventTypeId}/properties";
194
195
        $data['json'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
196
            'name'           => $name,
197
            'label'          => $label,
198
            'propertyType'   => $propertyType,
199
            'objectProperty' => $objectProperty,
200
            'options'        => $options,
201
        ];
202
203
        return $this->client->request('post', $endpoint, $data);
204
    }
205
206
    /**
207
     * Update Property for Timeline Event Type
208
     *
209
     * @param int        $appId
210
     * @param int        $eventTypeId
211
     * @param int        $eventTypePropertyId
212
     * @param string     $name
213
     * @param string     $label
214
     * @param string     $propertyType
215
     * @param array|null $options
216
     *
217
     * @return mixed
218
     *
219
     * @see http://developers.hubspot.com/docs/methods/timeline/udpate-timeline-event-type-property
220
     */
221
    public function updateEventTypeProperty(
222
        $appId,
223
        $eventTypeId,
224
        $eventTypePropertyId,
225
        $name,
226
        $label,
227
        $propertyType,
228
        $options = null
229
    ) {
230
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types/{$eventTypeId}/properties";
231
232
        $data['json'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
233
            'id'           => $eventTypePropertyId,
234
            'name'         => $name,
235
            'label'        => $label,
236
            'propertyType' => $propertyType,
237
        ];
238
239
        if (isset($options)) {
240
            $data['json']['options'] = $options;
241
        }
242
243
        return $this->client->request('put', $endpoint, $data);
244
    }
245
246
    /**
247
     * Delete Property for Timeline Event Type
248
     *
249
     * @param int $appId
250
     * @param int $eventTypeId
251
     * @param int $eventTypePropertyId
252
     *
253
     * @return mixed
254
     *
255
     * @see http://developers.hubspot.com/docs/methods/timeline/delete-timeline-event-type-property
256
     */
257
    public function deleteEventTypeProperty($appId, $eventTypeId, $eventTypePropertyId)
258
    {
259
        $endpoint = "https://api.hubapi.com/integrations/v1/{$appId}/timeline/event-types/{$eventTypeId}/properties/{$eventTypePropertyId}";
260
        return $this->client->request('delete', $endpoint);
261
    }
262
}
263