Webhooks   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 26.09 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 24
loc 92
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscription() 0 6 1
A createSubscription() 8 8 1
A updateSubscription() 8 8 1
A deleteSubscription() 0 6 1
A viewSettings() 0 6 1
A updateSettings() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SevenShores\Hubspot\Resources;
4
5
class Webhooks extends Resource
6
{
7
    /**
8
     * Get list of subscriptions.
9
     *
10
     * @param int $app_id
11
     * @return \SevenShores\Hubspot\Http\Response
12
     */    
13
    function getSubscription($app_id)
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...
14
    {
15
        $endpoint = "https://api.hubapi.com/webhooks/v1/{$app_id}/subscriptions";
16
17
        return $this->client->request('get', $endpoint);
18
    }
19
20
    /**
21
     * Create a new subscription.
22
     *
23
     * @param int   $app_id
24
     * @param array $subscription
25
     * @return \SevenShores\Hubspot\Http\Response
26
     */    
27 View Code Duplication
    function createSubscription($app_id, $subscription)
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...
28
    {
29
        $endpoint = "https://api.hubapi.com/webhooks/v1/{$app_id}/subscriptions";
30
31
        $options['json'] = $subscription;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = 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...
32
33
        return $this->client->request('post', $endpoint, $options);
34
    }
35
36
    /**
37
     * Update a subscription.
38
     *
39
     * @param int   $app_id
40
     * @param int   $subscription_id
41
     * @param array $subscription
42
     * @return \SevenShores\Hubspot\Http\Response
43
     */    
44 View Code Duplication
    function updateSubscription($app_id, $subscription_id, $subscription)
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...
45
    {
46
        $endpoint = "https://api.hubapi.com/webhooks/v1/{$app_id}/subscriptions/{$subscription_id}";
47
48
        $options['json'] = $subscription;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = 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...
49
50
        return $this->client->request('put', $endpoint, $options);
51
    }
52
53
    /**
54
     * Delete a subscription.
55
     *
56
     * @param int $app_id
57
     * @param int $subscription_id
58
     * @return \SevenShores\Hubspot\Http\Response
59
     */    
60
    function deleteSubscription($app_id, $subscription_id)
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...
61
    {
62
        $endpoint = "https://api.hubapi.com/webhooks/v1/{$app_id}/subscriptions/{$subscription_id}";
63
64
        return $this->client->request('delete', $endpoint);
65
    }
66
67
    /**
68
     * Get webhook settings.
69
     *
70
     * @param int $app_id
71
     * @return \SevenShores\Hubspot\Http\Response
72
     */    
73
    function viewSettings($app_id)
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...
74
    {
75
        $endpoint = "https://api.hubapi.com/webhooks/v1/{$app_id}/settings";
76
77
        return $this->client->request('get', $endpoint);
78
    }
79
80
    /**
81
     * Update webhook settings.
82
     *
83
     * @param int   $app_id
84
     * @param array $settings
85
     * @return \SevenShores\Hubspot\Http\Response
86
     */    
87 View Code Duplication
    function updateSettings($app_id, $settings)
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...
88
    {
89
        $endpoint = "https://api.hubapi.com/webhooks/v1/{$app_id}/settings";
90
91
        $options['json'] = $settings;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = 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
93
        return $this->client->request('put', $endpoint, $options);
94
    }
95
96
}
97