Events::trigger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace SevenShores\Hubspot\Resources;
4
5
class Events extends Resource
6
{
7
    /**
8
     * Trigger a custom event.
9
     *
10
     * This only works for enterprise accounts.
11
     *
12
     * @param  string $hubId               Your HubSpot portal ID ("Hub ID"). You can find your Hub ID in the
13
     *                                     footer of the HubSpot UI, or in the URL. For example, in this URL:
14
     *                                     "https://app.hubspot.com/reports/56043/events/" your Hub ID is "56043".
15
     * @param  string $eventId
16
     * @param  string $contactEmail        Contact email triggering the event.
17
     * @param  float  $contactRevenue      Optional - the monetary value this event means to you.
18
     * @param  array  $contactProperties   Optional - array of new contact properties.
19
     * @return \SevenShores\Hubspot\Http\Response
20
     */
21
    function trigger(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
        $hubId,
23
        $eventId,
24
        $contactEmail = null,
25
        $contactRevenue = null,
26
        $contactProperties = []
27
    ) {
28
        $endpoint = "http://track.hubspot.com/v1/event";
29
30
        $required['_a'] = $hubId;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$required was never initialized. Although not strictly required by PHP, it is generally a good practice to add $required = 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...
31
        $required['_n'] = $eventId;
32
        $required['email'] = $contactEmail;
33
34
        $parameters = array_merge(
35
          $required,
36
          ['_m' => $contactRevenue],
37
          $contactProperties
38
        );
39
40
        $query_string = build_query_string($parameters);
41
42
        return $this->client->request('get', $endpoint, [], $query_string, false);
43
    }
44
}
45