Passed
Push — master ( 176fe7...831cd8 )
by
unknown
01:04 queued 11s
created

AbstractApiClient::getUserInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Attachment;
9
use MovingImage\Client\VMPro\Entity\Channel;
10
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters;
11
use MovingImage\Client\VMPro\Entity\EmbedCode;
12
use MovingImage\Client\VMPro\Entity\Keyword;
13
use MovingImage\Client\VMPro\Entity\UserInfo;
14
use MovingImage\Client\VMPro\Entity\Video;
15
use MovingImage\Client\VMPro\Entity\VideoDownloadUrl;
16
use MovingImage\Client\VMPro\Entity\VideoManager;
17
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
18
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
19
use MovingImage\Client\VMPro\Entity\Thumbnail;
20
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
21
use MovingImage\Client\VMPro\Util\ChannelTrait;
22
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait;
23
use MovingImage\Client\VMPro\Util\SearchEndpointTrait;
24
25
/**
26
 * Class AbstractApiClient.
27
 *
28
 * @author Ruben Knol <[email protected]>
29
 * @author Omid Rad <[email protected]>
30
 */
31
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
32
{
33
    use LoggerAwareTrait;
34
    use SearchEndpointTrait;
35
    use ChannelTrait;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getChannels($videoManagerId)
41
    {
42
        $response = $this->makeRequest('GET', 'channels', [
43
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
44
        ]);
45
46
        $rootChannel = $this->deserialize($response->getBody(), Channel::class);
47
        $rootChannel->setChildren($this->sortChannels($rootChannel->getChildren()));
48
49
        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...
50
    }
51
52
    /**
53
     * Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side.
54
     *
55
     * @param ArrayCollection $channels
56
     *
57
     * @return ArrayCollection
58
     */
59
    protected function sortChannels(ArrayCollection $channels)
60
    {
61
        $channels->map(function ($channel) {
62
            $channel->setChildren($this->sortChannels($channel->getChildren()));
63
        });
64
65
        $iterator = $channels->getIterator();
66
        $iterator->uasort(function ($a, $b) {
67
            return $a->getName() > $b->getName();
68
        });
69
70
        return new ArrayCollection(iterator_to_array($iterator));
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function createVideo(
77
        $videoManagerId,
78
        $fileName,
79
        $title = '',
80
        $description = '',
81
        $channel = null,
82
        $group = null,
83
        array $keywords = [],
84
        $autoPublish = null
85
    ) {
86
        $response = $this->makeRequest('POST', 'videos', [
87
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
88
            'json' => $this->buildJsonParameters(
89
                compact('fileName'), // Required parameters
90
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
91
            ),
92
        ]);
93
94
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
95
        // wraps headers in arrays.
96
        $videoLocation = is_array($response->getHeader('location'))
97
            ? $response->getHeader('location')[0]
98
            : $response->getHeader('location');
99
100
        $pieces = explode('/', $videoLocation);
101
102
        return end($pieces);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
109
    {
110
        $options = [
111
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
112
        ];
113
114 View Code Duplication
        if ($parameters) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
115
            $query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986);
116
            $options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
117
        }
118
119
        $response = $this->makeRequest('GET', 'videos', $options);
120
        $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']);
121
122
        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...
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getCount($videoManagerId, VideosRequestParameters $parameters = null)
129
    {
130
        $options = [
131
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
132
        ];
133
134 View Code Duplication
        if ($parameters) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
135
            $query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986);
136
            $options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
137
        }
138
139
        $response = $this->makeRequest('GET', 'videos', $options);
140
141
        return json_decode($response->getBody()->getContents(), true)['total'];
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getVideoUploadUrl($videoManagerId, $videoId)
148
    {
149
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
150
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
151
        ]);
152
153
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
154
        // wraps headers in arrays.
155
        return is_array($response->getHeader('location'))
156
            ? $response->getHeader('location')[0]
157
            : $response->getHeader('location');
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function updateVideo($videoManagerId, $videoId, $title, $description, $autoPublish = null)
164
    {
165
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
166
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
167
            'json' => $this->buildJsonParameters([], compact('title', 'description', 'autoPublish')),
168
        ]);
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
175
    {
176
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
177
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
178
        ]);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function removeVideoFromChannel($videoManagerId, $videoId, $channelId)
185
    {
186
        $this->makeRequest('DELETE', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
187
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
188
        ]);
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
195
    {
196
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
197
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
198
            'json' => $metadata,
199
        ]);
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
206
    {
207
        $url = sprintf(
208
            'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
209
            $videoId,
210
            $playerDefinitionId,
211
            $embedType
212
        );
213
214
        if ($this->cacheTtl) {
215
            $url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl);
216
        }
217
218
        $response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]);
219
220
        $data = \json_decode($response->getBody(), true);
221
        $embedCode = new EmbedCode();
222
        $embedCode->setCode($data['embedCode']);
223
224
        return $embedCode;
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230
    public function deleteVideo($videoManagerId, $videoId)
231
    {
232
        $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [
233
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
234
        ]);
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function getVideo($videoManagerId, $videoId, VideoRequestParameters $parameters = null)
241
    {
242
        $options = [
243
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
244
        ];
245
246
        if ($parameters) {
247
            $options['query'] = $parameters->getContainer();
248
        }
249
250
        $response = $this->makeRequest(
251
            'GET',
252
            sprintf('videos/%s', $videoId),
253
            $options
254
        );
255
256
        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...
257
    }
258
259
    /**
260
     * {@inheritdoc}
261
     */
262 View Code Duplication
    public function getAttachments($videoManagerId, $videoId)
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...
263
    {
264
        $response = $this->makeRequest(
265
            'GET',
266
            sprintf('videos/%s/attachments', $videoId),
267
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
268
        );
269
270
        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...
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 View Code Duplication
    public function getChannelAttachments($videoManagerId, $channelId)
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...
277
    {
278
        $response = $this->makeRequest(
279
            'GET',
280
            sprintf('channels/%s/attachments', $channelId),
281
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
282
        );
283
284
        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...::getChannelAttachments 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...
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290 View Code Duplication
    public function getKeywords($videoManagerId, $videoId)
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...
291
    {
292
        $uri = is_null($videoId)
293
            ? 'keyword/find'
294
            : sprintf('videos/%s/keywords', $videoId);
295
296
        $response = $this->makeRequest(
297
            'GET',
298
            $uri,
299
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
300
        );
301
302
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Keyword::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...\Keyword::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...tInterface::getKeywords of type MovingImage\Client\VMPro\Entity\Keyword[].

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...
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308
    public function updateKeywords($videoManagerId, $videoId, $keywords)
309
    {
310
        //remove all keywords
311
        foreach ($this->getKeywords($videoManagerId, $videoId) as $keyword) {
0 ignored issues
show
Bug introduced by
The expression $this->getKeywords($videoManagerId, $videoId) of type object|array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
312
            $this->deleteKeyword($videoManagerId, $videoId, $keyword->getId());
313
        }
314
315
        //add new
316
        foreach ($keywords as $keyword) {
317
            $this->makeRequest('POST', sprintf('videos/%s/keywords', $videoId), [
318
                self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
319
                'json' => ['text' => $keyword],
320
            ]);
321
        }
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327
    public function deleteKeyword($videoManagerId, $videoId, $keywordId)
328
    {
329
        $this->makeRequest('DELETE', sprintf('videos/%s/keywords/%s', $videoId, $keywordId), [
330
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
331
        ]);
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     */
337
    public function searchVideos($videoManagerId, VideosRequestParameters $parameters = null, $searchQuery = null)
338
    {
339
        $options = $this->getRequestOptionsForSearchVideosEndpoint($videoManagerId, $parameters);
340
        if ($searchQuery) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchQuery 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...
341
            $options['query'] = sprintf('(%s) AND (%s)', $options['query'], $searchQuery);
342
        }
343
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
344
        $response = $this->normalizeSearchVideosResponse($response->getBody()->getContents());
345
346
        $collection = $this->deserialize($response, VideoCollection::class);
347
348
        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...
349
    }
350
351
    /**
352
     * {@inheritdoc}
353
     */
354
    public function searchChannels($videoManagerId, ChannelsRequestParameters $parameters = null)
355
    {
356
        $options = $this->getRequestOptionsForSearchChannelsEndpoint($videoManagerId, $parameters);
357
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
358
        $response = $this->normalizeSearchChannelsResponse($response->getBody()->getContents());
359
        /** @var ChannelCollection $collection */
360
        $collection = $this->deserialize($response, ChannelCollection::class);
361
362
        //builds parent/children relations on all channels
363
        $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...
364
        $collection->setChannels($channels);
365
366
        return $collection;
367
    }
368
369
    /**
370
     * {@inheritdoc}
371
     */
372
    public function getVideoManagers()
373
    {
374
        $response = $this->makeRequest('GET', '', []);
375
376
        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...
377
    }
378
379
    /**
380
     * {@inheritdoc}
381
     */
382 View Code Duplication
    public function getVideoDownloadUrls($videoManagerId, $videoId)
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...
383
    {
384
        $options = [
385
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
386
        ];
387
388
        $response = $this->makeRequest(
389
            'GET',
390
            sprintf('videos/%s/download-urls', $videoId),
391
            $options
392
        );
393
394
        $response = $response->getBody()->getContents();
395
396
        return $this->deserialize($response, 'ArrayCollection<'.VideoDownloadUrl::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...nloadUrl::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...e::getVideoDownloadUrls of type MovingImage\Client\VMPro\Entity\VideoDownloadUrl[].

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...
397
    }
398
399
    /**
400
     * {@inheritdoc}
401
     */
402
    public function createThumbnailByTimestamp($videoManagerId, $videoId, $timestamp)
403
    {
404
        $options = [
405
            self::OPT_VIDEO_MANAGER_ID => intval($videoManagerId),
406
        ];
407
408
        $response = $this->makeRequest(
409
            'POST',
410
            'videos/'.$videoId.'/thumbnails?timestamp='.intval($timestamp),
411
            $options
412
        );
413
414
        if (preg_match('/\/thumbnails\/([0-9]*)/', $response->getHeader('Location')[0], $match)) {
415
            return (new Thumbnail())->setId(intval($match[1]));
416
        }
417
    }
418
419
    /**
420
     * {@inheritdoc}
421
     */
422
    public function getThumbnail($videoManagerId, $videoId, $thumbnailId)
423
    {
424
        $options = [
425
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
426
        ];
427
428
        $response = $this->makeRequest(
429
            'GET',
430
            'videos/'.$videoId.'/thumbnails/'.$thumbnailId.'/url',
431
            $options
432
        );
433
434
        $result = \json_decode($response->getBody()->getContents(), true);
435
436
        if (isset($result['downloadUrl'])) {
437
            return (new Thumbnail())
438
                ->setId(intval($thumbnailId))
439
                ->setUrl($result['downloadUrl']);
440
        }
441
442
        return null;
443
    }
444
445
    /**
446
     * {@inheritdoc}
447
     */
448
    public function updateThumbnail($videoManagerId, $videoId, $thumbnailId, $active)
449
    {
450
        $options = [
451
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
452
            'json' => ['active' => boolval($active)],
453
        ];
454
455
        $this->makeRequest(
456
            'PATCH',
457
            'videos/'.$videoId.'/thumbnails/'.intval($thumbnailId),
458
            $options
459
        );
460
    }
461
462
    /**
463
     * {@inheritdoc}
464
     */
465
    public function getUserInfo($idToken)
466
    {
467
        $options = [
468
            'body' => json_encode(['jwt_id_token' => $idToken]),
469
            'headers' => [
470
                'Content-Type' => 'application/json',
471
            ],
472
        ];
473
474
        $response = $this->makeRequest('POST', 'corp-tube-admin/user-info', $options);
475
476
        return $this->deserialize($response->getBody()->getContents(), UserInfo::class);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...ntity\UserInfo::class); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...tInterface::getUserInfo of type MovingImage\Client\VMPro\Entity\UserInfo.

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...
477
    }
478
}
479