Files::move()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SevenShores\Hubspot\Resources;
4
5
class Files extends Resource
6
{
7
    /**
8
     * Upload a new file.
9
     *
10
     * @param string $file   File path
11
     * @param array  $params Optional parameters
12
     * @return \SevenShores\Hubspot\Http\Response
13
     */
14
    function upload($file, $params = [])
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...
15
    {
16
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files";
17
18
        $queryString = build_query_string([
19
            'overwrite' => isset($params['overwrite']) ? $params['overwrite'] : false,
20
        ]);
21
22
        $options['multipart'] = [
23
            [
24
                'name' => 'files',
25
                'contents' => fopen($file, 'rb')
26
            ],
27
            [
28
                'name' => 'file_names',
29
                'contents' => isset($params['file_names']) ? $params['file_names'] : null
30
            ],[
31
                'name' => 'folder_paths',
32
                'contents' => isset($params['folder_paths']) ? $params['folder_paths'] : null
33
            ]
34
        ];
35
36
        return $this->client->request('post', $endpoint, $options, $queryString);
37
    }
38
39
    /**
40
     * Get meta data for all files.
41
     *
42
     * @param array $params Optional parameters
43
     * @return \SevenShores\Hubspot\Http\Response
44
     */
45
    function all($params = [])
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...
46
    {
47
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files";
48
49
        $queryString = build_query_string($params);
50
51
        return $this->client->request('get', $endpoint, [], $queryString);
52
    }
53
54
55
    /**
56
     * Upload a replacement file.
57
     *
58
     * @param int    $file_id The file ID
59
     * @param string $file    The file path
60
     * @return \SevenShores\Hubspot\Http\Response
61
     */
62
    function replace($file_id, $file)
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...
63
    {
64
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files/{$file_id}";
65
66
        $options['multipart'] = [
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...
67
            [
68
                'name' => 'files',
69
                'contents' => fopen($file, 'rb')
70
            ]
71
        ];
72
73
        return $this->client->request('post', $endpoint, $options);
74
    }
75
76
    /**
77
     * Get file metadata.
78
     *
79
     * @param $file_id
80
     * @return \SevenShores\Hubspot\Http\Response
81
     */
82
    function meta($file_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...
83
    {
84
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files/{$file_id}";
85
86
        return $this->client->request('get', $endpoint);
87
    }
88
89
    /**
90
     * Archive a file.
91
     *
92
     * @param int $file_id The file ID
93
     * @return \SevenShores\Hubspot\Http\Response
94
     */
95
    function archive($file_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...
96
    {
97
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files/{$file_id}/archive";
98
99
        return $this->client->request('post', $endpoint);
100
    }
101
102
    /**
103
     * Delete a file.
104
     *
105
     * @param int $file_id The file ID
106
     * @return \SevenShores\Hubspot\Http\Response
107
     */
108
    function delete($file_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...
109
    {
110
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files/{$file_id}";
111
112
        return $this->client->request('delete', $endpoint);
113
    }
114
115
    /**
116
     * Move a file to a new folder.
117
     *
118
     * Parameters:
119
     * folder_path  string    The path of the folder to move the file into. Use this OR folder_id - not both.
120
     * folder_id    string    The id of the folder to move the file into. Use this OR folder_path - not both.
121
     * name            string    The new name of the file.
122
     *
123
     * @param int   $file_id
124
     * @param array $params
125
     * @return \SevenShores\Hubspot\Http\Response
126
     */
127
    function move($file_id, $params = [])
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...
128
    {
129
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/files/{$file_id}/move-file";
130
131
        $options['json'] = $params;
132
133
        return $this->client->request('post', $endpoint, $options);
134
    }
135
136
    /**
137
     * Create a new folder.
138
     *
139
     * @param string $folder_name
140
     * @param int    $parent_folder_id
141
     * @return \SevenShores\Hubspot\Http\Response
142
     */
143 View Code Duplication
    function createFolder($folder_name, $parent_folder_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...
144
    {
145
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/folders";
146
147
        $options['json'] = [
148
            'name'             => $folder_name,
149
            'parent_folder_id' => $parent_folder_id,
150
        ];
151
152
        return $this->client->request('post', $endpoint, $options);
153
    }
154
155
    /**
156
     * List folders metadata.
157
     *
158
     * @param array $params
159
     * @return \SevenShores\Hubspot\Http\Response
160
     */
161
    function folders($params = [])
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...
162
    {
163
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/folders";
164
165
        $queryString = build_query_string($params);
166
167
        return $this->client->request('get', $endpoint, [], $queryString);
168
    }
169
170
    /**
171
     * Update a folder.
172
     *
173
     * @param int   $folder_id
174
     * @param array $params
175
     * @return \SevenShores\Hubspot\Http\Response
176
     */
177
    function updateFolder($folder_id, $params = [])
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...
178
    {
179
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/folders/{$folder_id}";
180
181
        $options['json'] = $params;
182
183
        return $this->client->request('put', $endpoint, $options);
184
    }
185
186
    /**
187
     * Delete a folder.
188
     *
189
     * @param int $folder_id
190
     * @return \SevenShores\Hubspot\Http\Response
191
     */
192
    function deleteFolder($folder_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...
193
    {
194
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/folders/{$folder_id}";
195
196
        return $this->client->request('delete', $endpoint);
197
    }
198
199
    /**
200
     * Get the folder by ID.
201
     *
202
     * @param int $folder_id
203
     * @return \SevenShores\Hubspot\Http\Response
204
     */
205
    function getFolderById($folder_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...
206
    {
207
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/folders/{$folder_id}";
208
209
        return $this->client->request('get', $endpoint);
210
    }
211
212
    /**
213
     * Move a folder.
214
     *
215
     * @param int   $folder_id
216
     * @param array $params
217
     * @return \SevenShores\Hubspot\Http\Response
218
     */
219
    function moveFolder($folder_id, $params = [])
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...
220
    {
221
        $endpoint = "https://api.hubapi.com/filemanager/api/v2/folders/{$folder_id}/move-folder";
222
223
        $options['json'] = $params;
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...
224
225
        return $this->client->request('post', $endpoint, $options);
226
    }
227
228
}
229