Album::removeImages()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
ccs 0
cts 13
cp 0
rs 9.4286
cc 2
eloc 9
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Mechpave\ImgurClient\Api;
4
5
use Mechpave\ImgurClient\Entity\AlbumInterface;
6
use Mechpave\ImgurClient\Http\HttpClientInterface;
7
use Mechpave\ImgurClient\Mapper\AlbumMapper;
8
9
/**
10
 * Class Album
11
 * @package Mechpave\ImgurClient\Api
12
 * @see http://api.imgur.com/endpoints/album
13
 */
14
class Album extends AbstractApi
15
{
16
    /**
17
     * @var AlbumMapper
18
     */
19
    protected $albumMapper;
20
21
    /**
22
     * Album constructor.
23
     * @param HttpClientInterface $httpClient
24
     * @param AlbumMapper $albumMapper
25
     */
26
    public function __construct(HttpClientInterface $httpClient, AlbumMapper $albumMapper)
27
    {
28
        parent::__construct($httpClient);
29
        $this->albumMapper = $albumMapper;
30
    }
31
32
    /**
33
     * Get information about a specific album
34
     *
35
     * @param $id
36
     * @return AlbumInterface
37
     */
38
    public function get($id)
39
    {
40
        $response = $this->httpClient->get(
41
            'album/' . $id
42
        );
43
44
        $album = $this->albumMapper->buildAlbum($response->getBody()['data']);
45
46
        return $album;
47
    }
48
49
    /**
50
     * Create a new album
51
     *
52
     * @param array $ids Ids of the images you want to add to the album
53
     * @param string $title
54
     * @param string $description
55
     * @param string $privacy
56
     * @param string $layout
57
     * @param string $cover
58
     * @return AlbumInterface
59
     */
60
    public function create(
61
        $ids = [],
62
        $title = null,
63
        $description = null,
64
        $privacy = null,
65
        $layout = null,
66
        $cover = null
67
    )
68
    {
69
        $postData = [];
70
71
        if (!empty($ids)) {
72
            $postData['ids'] = implode(',', $ids);
73
        }
74
        if($title) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
75
            $postData['title'] = $title;
76
        }
77
        if($description) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $description of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
78
            $postData['description'] = $description;
79
        }
80
        if($privacy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $privacy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
81
            $postData['privacy'] = $privacy;
82
        }
83
        if($layout) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $layout of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
            $postData['layout'] = $layout;
85
        }
86
        if($cover) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cover of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
87
            $postData['cover'] = $cover;
88
        }
89
90
        $response = $this->httpClient->post(
91
            'album',
92
            $postData
93
        );
94
95
        $album = $this->albumMapper->buildAlbum($response->getBody()['data']);
96
        $album->setTitle($title);
97
        $album->setDescription($description);
98
        $album->setPrivacy($privacy);
99
        $album->setLayout($layout);
100
        $album->setCover($cover);
101
102
        return $album;
103
    }
104
105
    /**
106
     * Update album
107
     *
108
     * @param string $id Imgur Id of the album
109
     * @param array $ids Ids of the images you want to add to the album
110
     * @param string $title
111
     * @param string $description
112
     * @param string $privacy
113
     * @param string $layout
114
     * @param string $cover
115
     * @return bool
116
     */
117
    public function update(
118
        $id,
119
        $ids = [],
120
        $title = null,
121
        $description = null,
122
        $privacy = null,
123
        $layout = null,
124
        $cover = null
125
    )
126
    {
127
        $postData = [];
128
129
        if (!empty($ids)) {
130
            $postData['ids'] = implode(',', $ids);
131
        }
132
        if($title) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
133
            $postData['title'] = $title;
134
        }
135
        if($description) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $description of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
136
            $postData['description'] = $description;
137
        }
138
        if($privacy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $privacy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
139
            $postData['privacy'] = $privacy;
140
        }
141
        if($layout) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $layout of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
142
            $postData['layout'] = $layout;
143
        }
144
        if($cover) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cover of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
145
            $postData['cover'] = $cover;
146
        }
147
148
        $response = $this->httpClient->post(
149
            'album/' . $id,
150
            $postData
151
        );
152
153
        return $response->getBody()['success'];
154
    }
155
156
    /**
157
     * Delete an album
158
     *
159
     * @param string $id Imgur Id or deletehash of the album
160
     * @return bool
161
     */
162
    public function delete($id)
163
    {
164
        $response = $this->httpClient->delete(
165
            'album/' . $id
166
        );
167
168
        return $response->getBody()['success'];
169
    }
170
171
    /**
172
     * Favorite an album
173
     *
174
     * @param string $id
175
     * @return bool
176
     */
177
    public function favorite($id)
178
    {
179
        $response = $this->httpClient->post(
180
            'album/' . $id . '/favorite'
181
        );
182
183
        return $response->getBody()['success'];
184
    }
185
186
    /**
187
     * Sets the images for an album, removes all other images and only uses the images in this request.
188
     *
189
     * @param string $id
190
     * @param array $ids
191
     * @return bool
192
     */
193 View Code Duplication
    public function setImages($id, array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        if (empty($ids)) {
196
            throw new \InvalidArgumentException('Images ids must be not empty array');
197
        }
198
199
        $postData = [];
200
        $postData['ids'] = implode(',', $ids);
201
202
        $response = $this->httpClient->post(
203
            'album/' . $id,
204
            $postData
205
        );
206
207
        return $response->getBody()['success'];
208
    }
209
210
    /**
211
     * Adds images to an album
212
     *
213
     * @param string $id
214
     * @param array $ids
215
     * @return bool
216
     */
217 View Code Duplication
    public function addImages($id, array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
    {
219
        if (empty($ids)) {
220
            throw new \InvalidArgumentException('Images ids must be not empty array');
221
        }
222
223
        $postData = [];
224
        $postData['ids'] = implode(',', $ids);
225
226
        $response = $this->httpClient->post(
227
            'album/' . $id . '/add',
228
            $postData
229
        );
230
231
        return $response->getBody()['success'];
232
    }
233
234
    /**
235
     * Removes images from an album
236
     *
237
     * @param string $id
238
     * @param array $ids
239
     * @return bool
240
     */
241 View Code Duplication
    public function removeImages($id, array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        if (empty($ids)) {
244
            throw new \InvalidArgumentException('Images ids must be not empty array');
245
        }
246
247
        $postData = [];
248
        $postData['ids'] = implode(',', $ids);
249
250
        $response = $this->httpClient->delete(
251
            'album/' . $id . '/remove_images',
252
            $postData
0 ignored issues
show
Unused Code introduced by
The call to HttpClientInterface::delete() has too many arguments starting with $postData.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
253
        );
254
255
        return $response->getBody()['success'];
256
    }
257
}