Completed
Pull Request — master (#177)
by Craig
01:54 queued 13s
created

Forms::getSubmissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SevenShores\Hubspot\Resources;
4
5
class Forms extends Resource
6
{
7
    /**
8
     * Submit data to a form.
9
     *
10
     * @see http://developers.hubspot.com/docs/methods/forms/submit_form
11
     *
12
     * Send form submission data to HubSpot. Form submissions from external sources can be made to any registered
13
     * HubSpot form. You can see a list of forms on your portal by going to the Contacts > Forms page
14
     *
15
     * @param int    $portal_id
16
     * @param string $form_guid
17
     * @param array  $form
18
     *
19
     * @return \SevenShores\Hubspot\Http\Response
20
     */
21
    public function submit($portal_id, $form_guid, $form)
22
    {
23
        $endpoint = "https://forms.hubspot.com/uploads/form/v2/{$portal_id}/{$form_guid}";
24
25
        $options['form_params'] = $form;
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...
26
27
        return $this->client->request('post', $endpoint, $options, null, false);
28
    }
29
30
    /**
31
     * Return all forms that have been created in the portal.
32
     *
33
     * @see http://developers.hubspot.com/docs/methods/forms/v2/get_forms
34
     *
35
     * @return \SevenShores\Hubspot\Http\Response
36
     */
37
    public function all()
38
    {
39
        $endpoint = 'https://api.hubapi.com/forms/v2/forms';
40
41
        return $this->client->request('get', $endpoint);
42
    }
43
44
    /**
45
     * Return a single form based on the unique ID of that form.
46
     *
47
     * @see http://developers.hubspot.com/docs/methods/forms/v2/get_form
48
     *
49
     * @param string $form_guid
50
     *
51
     * @return \SevenShores\Hubspot\Http\Response
52
     */
53
    public function getById($form_guid)
54
    {
55
        $endpoint = "https://api.hubapi.com/forms/v2/forms/{$form_guid}";
56
57
        return $this->client->request('get', $endpoint);
58
    }
59
60
    /**
61
     * Create a new form.
62
     *
63
     * @see http://developers.hubspot.com/docs/methods/forms/v2/create_form
64
     *
65
     * @param array $form
66
     *
67
     * @return \SevenShores\Hubspot\Http\Response
68
     */
69
    public function create($form)
70
    {
71
        $endpoint = 'https://api.hubapi.com/forms/v2/forms';
72
73
        $options['json'] = $form;
74
75
        return $this->client->request('post', $endpoint, $options);
76
    }
77
78
    /**
79
     * Update an existing form.
80
     *
81
     * @see http://developers.hubspot.com/docs/methods/forms/v2/update_form
82
     *
83
     * @param string $form_guid
84
     * @param array  $form
85
     *
86
     * @return \SevenShores\Hubspot\Http\Response
87
     */
88
    public function update($form_guid, $form)
89
    {
90
        $endpoint = "https://api.hubapi.com/forms/v2/forms/{$form_guid}";
91
92
        $options['json'] = $form;
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...
93
94
        return $this->client->request('post', $endpoint, $options);
95
    }
96
97
    /**
98
     * Delete an existing form.
99
     *
100
     * @see http://developers.hubspot.com/docs/methods/forms/v2/delete_form
101
     *
102
     * @param string $form_guid
103
     *
104
     * @return \SevenShores\Hubspot\Http\Response
105
     */
106
    public function delete($form_guid)
107
    {
108
        $endpoint = "https://api.hubapi.com/forms/v2/forms/{$form_guid}";
109
110
        return $this->client->request('delete', $endpoint);
111
    }
112
113
    /**
114
     * Get all fields from a form.
115
     *
116
     * @see http://developers.hubspot.com/docs/methods/forms/v2/get_fields
117
     *
118
     * @param string $form_guid
119
     *
120
     * @return \SevenShores\Hubspot\Http\Response
121
     */
122
    public function getFields($form_guid)
123
    {
124
        $endpoint = "https://api.hubapi.com/forms/v2/fields/{$form_guid}";
125
126
        return $this->client->request('get', $endpoint);
127
    }
128
129
    /**
130
     * Get all fields from a form.
131
     *
132
     * @see https://developers.hubspot.com/docs/methods/forms/get-submissions-for-a-form
133
     *
134
     * @param string $form_guid
135
     *
136
     * @return \SevenShores\Hubspot\Http\Response
137
     */
138
    public function getSubmissions($form_guid)
139
    {
140
        $endpoint = "https://api.hubapi.com/form-integrations/v1/submissions/forms/{$form_guid}";
141
142
        return $this->client->request('get', $endpoint);
143
    }
144
145
    /**
146
     * Get a single field from a form.
147
     *
148
     * @see http://developers.hubspot.com/docs/methods/forms/v2/get_field
149
     *
150
     * @param string $form_guid
151
     * @param string $name
152
     *
153
     * @return \SevenShores\Hubspot\Http\Response
154
     */
155
    public function getFieldByName($form_guid, $name)
156
    {
157
        $endpoint = "https://api.hubapi.com/forms/v2/fields/{$form_guid}/{$name}";
158
159
        return $this->client->request('get', $endpoint);
160
    }
161
}
162