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

Workflows   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 10
loc 116
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 6 1
A getById() 0 6 1
A enrollContact() 0 6 1
A unenrollContact() 0 6 1
A create() 0 8 1
A delete() 0 6 1
A enrollmentsForContact() 0 6 1
A logEvents() 10 10 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 Workflows extends Resource
6
{
7
    /**
8
     * Get all workflows.
9
     *
10
     * @return \SevenShores\Hubspot\Http\Response
11
     */
12
    public function all()
13
    {
14
        $endpoint = 'https://api.hubapi.com/automation/v3/workflows';
15
16
        return $this->client->request('get', $endpoint);
17
    }
18
19
    /**
20
     * Get a specific workflow.
21
     *
22
     * @param int $id
23
     * @return \SevenShores\Hubspot\Http\Response
24
     */
25
    public function getById($id)
26
    {
27
        $endpoint = "https://api.hubapi.com/automation/v3/workflows/{$id}";
28
29
        return $this->client->request('get', $endpoint);
30
    }
31
32
    /**
33
     * Enroll a contact in a workflow.
34
     *
35
     * @param int $workflow_id
36
     * @param string $email
37
     * @return \SevenShores\Hubspot\Http\Response
38
     */
39
    public function enrollContact($workflow_id, $email)
40
    {
41
        $endpoint = "https://api.hubapi.com/automation/v2/workflows/{$workflow_id}/enrollments/contacts/{$email}";
42
43
        return $this->client->request('post', $endpoint);
44
    }
45
46
    /**
47
     * Unenroll a contact from a workflow.
48
     *
49
     * @param int $workflow_id
50
     * @param string $email
51
     * @return \SevenShores\Hubspot\Http\Response
52
     */
53
    public function unenrollContact($workflow_id, $email)
54
    {
55
        $endpoint = "https://api.hubapi.com/automation/v2/workflows/{$workflow_id}/enrollments/contacts/{$email}";
56
57
        return $this->client->request('delete', $endpoint);
58
    }
59
60
    /**
61
     * Create a new workflow.
62
     *
63
     * @param array $workflow The workflow properties
64
     * @return \SevenShores\Hubspot\Http\Response
65
     */
66
    public function create($workflow)
67
    {
68
        $endpoint = 'https://api.hubapi.com/automation/v3/workflows';
69
70
        $options['json'] = $workflow;
71
72
        return $this->client->request('post', $endpoint, $options);
73
    }
74
75
    /**
76
     * Delete a workflow.
77
     *
78
     * @param int $id
79
     * @return \SevenShores\Hubspot\Http\Response
80
     */
81
    public function delete($id)
82
    {
83
        $endpoint = "https://api.hubapi.com/automation/v3/workflows/{$id}";
84
85
        return $this->client->request('delete', $endpoint, []);
86
    }
87
88
    /**
89
     * Get current enrollments for a contact.
90
     *
91
     * @param int $contact_id
92
     * @return \SevenShores\Hubspot\Http\Response
93
     */
94
    public function enrollmentsForContact($contact_id)
95
    {
96
        $endpoint = "https://api.hubapi.com/automation/v2/workflows/enrollments/contacts/{$contact_id}";
97
98
        return $this->client->request('get', $endpoint);
99
    }
100
101
    /**
102
     * Get a history of events for a specific workflow, filtered for a
103
     * specific contact and/or event type(s).
104
     *
105
     * @param int $workflow_id
106
     * @param array $filter
107
     * @param array $params Optional parameters.
108
     * @return \SevenShores\Hubspot\Http\Response
109
     */
110 View Code Duplication
    public function logEvents($workflow_id, $filter, $params = [])
111
    {
112
        $endpoint = "https://api.hubapi.com/automation/v3/logevents/workflows/{$workflow_id}/filter";
113
114
        $options['json'] = $filter;
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...
115
116
        $queryString = build_query_string($params);
117
118
        return $this->client->request('put', $endpoint, $options, $queryString);
119
    }
120
}
121