Test Setup Failed
Push — master ( 5a41c6...66867e )
by Omid
05:34
created

AbstractApiClient::getVideoManagers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace MovingImage\Client\VMPro\ApiClient;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use MovingImage\Client\VMPro\Collection\ChannelCollection;
7
use MovingImage\Client\VMPro\Collection\VideoCollection;
8
use MovingImage\Client\VMPro\Entity\Channel;
9
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters;
10
use MovingImage\Client\VMPro\Entity\EmbedCode;
11
use MovingImage\Client\VMPro\Entity\Video;
12
use MovingImage\Client\VMPro\Entity\Attachment;
13
use MovingImage\Client\VMPro\Entity\VideoManager;
14
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
15
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
16
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
17
use MovingImage\Client\VMPro\Util\ChannelTrait;
18
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait;
19
use MovingImage\Client\VMPro\Util\SearchEndpointTrait;
20
21
/**
22
 * Class AbstractApiClient.
23
 *
24
 * @author Ruben Knol <[email protected]>
25
 * @author Omid Rad <[email protected]>
26
 */
27
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
28
{
29
    use LoggerAwareTrait;
30
    use SearchEndpointTrait;
31
    use ChannelTrait;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getChannels($videoManagerId)
37
    {
38
        $response = $this->makeRequest('GET', 'channels', [
39
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
40
        ]);
41
42
        $rootChannel = $this->deserialize($response->getBody(), Channel::class);
43
        $rootChannel->setChildren($this->sortChannels($rootChannel->getChildren()));
44
45
        return $rootChannel;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $rootChannel; (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...tInterface::getChannels of type MovingImage\Client\VMPro\Entity\Channel.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
    }
47
48
    /**
49
     * Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side.
50
     *
51
     * @param ArrayCollection $channels
52
     *
53
     * @return ArrayCollection
54
     */
55
    protected function sortChannels(ArrayCollection $channels)
56
    {
57
        $channels->map(function ($channel) {
58
            $channel->setChildren($this->sortChannels($channel->getChildren()));
59
        });
60
61
        $iterator = $channels->getIterator();
62
        $iterator->uasort(function ($a, $b) {
63
            return $a->getName() > $b->getName();
64
        });
65
66
        return new ArrayCollection(iterator_to_array($iterator));
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function createVideo(
73
        $videoManagerId,
74
        $fileName,
75
        $title = '',
76
        $description = '',
77
        $channel = null,
78
        $group = null,
79
        array $keywords = [],
80
        $autoPublish = null
81
    ) {
82
        $response = $this->makeRequest('POST', 'videos', [
83
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
84
            'json' => $this->buildJsonParameters(
85
                compact('fileName'), // Required parameters
86
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
87
            ),
88
        ]);
89
90
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
91
        // wraps headers in arrays.
92
        $videoLocation = is_array($response->getHeader('location'))
93
            ? $response->getHeader('location')[0]
94
            : $response->getHeader('location');
95
96
        $pieces = explode('/', $videoLocation);
97
98
        return end($pieces);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
105
    {
106
        $options = [
107
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
108
        ];
109
110
        if ($parameters) {
111
            $options['query'] = $parameters->getContainer();
112
        }
113
114
        $response = $this->makeRequest('GET', 'videos', $options);
115
        $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']);
116
117
        return $this->deserialize($response, 'ArrayCollection<'.Video::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...ty\Video::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...entInterface::getVideos of type MovingImage\Meta\Interfaces\VideoInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 View Code Duplication
    public function getCount($videoManagerId, VideosRequestParameters $parameters = null)
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...
124
    {
125
        $options = [
126
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
127
        ];
128
129
        if ($parameters) {
130
            $options['query'] = $parameters->getContainer();
131
        }
132
133
        $response = $this->makeRequest('GET', 'videos', $options);
134
135
        return json_decode($response->getBody()->getContents(), true)['total'];
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getVideoUploadUrl($videoManagerId, $videoId)
142
    {
143
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
144
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
145
        ]);
146
147
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
148
        // wraps headers in arrays.
149
        return is_array($response->getHeader('location'))
150
            ? $response->getHeader('location')[0]
151
            : $response->getHeader('location');
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function updateVideo($videoManagerId, $videoId, $title, $description)
158
    {
159
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
160
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
161
            'json' => $this->buildJsonParameters([], compact('title', 'description')),
162
        ]);
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
169
    {
170
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
171
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
172
        ]);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
179
    {
180
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
181
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
182
            'json' => $metadata,
183
        ]);
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
190
    {
191
        $url = sprintf(
192
            'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
193
            $videoId,
194
            $playerDefinitionId,
195
            $embedType
196
        );
197
198
        if ($this->cacheTtl) {
199
            $url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl);
200
        }
201
202
        $response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]);
203
204
        $data = \json_decode($response->getBody(), true);
205
        $embedCode = new EmbedCode();
206
        $embedCode->setCode($data['embedCode']);
207
208
        return $embedCode;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function deleteVideo($videoManagerId, $videoId)
215
    {
216
        $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [
217
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
218
        ]);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224 View Code Duplication
    public function getVideo($videoManagerId, $videoId, VideoRequestParameters $parameters = null)
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...
225
    {
226
        $options = [
227
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
228
        ];
229
230
        if ($parameters) {
231
            $options['query'] = $parameters->getContainer();
232
        }
233
234
        $response = $this->makeRequest(
235
            'GET',
236
            sprintf('videos/%s', $videoId),
237
            $options
238
        );
239
240
        return $this->deserialize($response->getBody()->getContents(), Video::class);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...o\Entity\Video::class); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...ientInterface::getVideo of type MovingImage\Client\VMPro\Entity\Video.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    public function getAttachments($videoManagerId, $videoId)
247
    {
248
        $response = $this->makeRequest(
249
            'GET',
250
            sprintf('videos/%s/attachments', $videoId),
251
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
252
        );
253
254
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Attachment::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...tachment::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...terface::getAttachments of type MovingImage\Client\VMPro\Entity\Attachment[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function searchVideos($videoManagerId, VideosRequestParameters $parameters = null)
261
    {
262
        $options = $this->getRequestOptionsForSearchVideosEndpoint($videoManagerId, $parameters);
263
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
264
        $response = $this->normalizeSearchVideosResponse($response->getBody()->getContents());
265
266
        $collection = $this->deserialize($response, VideoCollection::class);
267
268
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $collection; (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...Interface::searchVideos of type MovingImage\Client\VMPro...lection\VideoCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274
    public function searchChannels($videoManagerId, ChannelsRequestParameters $parameters = null)
275
    {
276
        $options = $this->getRequestOptionsForSearchChannelsEndpoint($videoManagerId, $parameters);
277
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
278
        $response = $this->normalizeSearchChannelsResponse($response->getBody()->getContents());
279
        /** @var ChannelCollection $collection */
280
        $collection = $this->deserialize($response, ChannelCollection::class);
281
282
        //builds parent/children relations on all channels
283
        $channels = $this->setChannelRelations($collection->getChannels());
0 ignored issues
show
Documentation introduced by
$collection->getChannels() is of type array<integer,object<Mov...aces\ChannelInterface>>, but the function expects a array<integer,object<Mov...\VMPro\Entity\Channel>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
284
        $collection->setChannels($channels);
285
286
        return $collection;
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292
    public function getVideoManagers()
293
    {
294
        $response = $this->makeRequest('GET', '', []);
295
296
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.VideoManager::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...oManager::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...rface::getVideoManagers of type MovingImage\Client\VMPro\Entity\VideoManager[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
297
    }
298
}
299