Completed
Pull Request — master (#27)
by
unknown
07:30
created

AbstractApiClient::getAttachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace MovingImage\Client\VMPro\ApiClient;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use MovingImage\Client\VMPro\Entity\Channel;
7
use MovingImage\Client\VMPro\Entity\EmbedCode;
8
use MovingImage\Client\VMPro\Entity\Video;
9
use MovingImage\Client\VMPro\Entity\Attachment;
10
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
11
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
12
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
13
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait;
14
15
/**
16
 * Class AbstractApiClient.
17
 *
18
 * @author Ruben Knol <[email protected]>
19
 * @author Omid Rad <[email protected]>
20
 */
21
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
22
{
23
    use LoggerAwareTrait;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getChannels($videoManagerId)
29
    {
30
        $response = $this->makeRequest('GET', 'channels', [
31
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
32
        ]);
33
34
        $rootChannel = $this->deserialize($response->getBody(), Channel::class);
35
        $rootChannel->setChildren($this->sortChannels($rootChannel->getChildren()));
36
37
        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...
38
    }
39
40
    /**
41
     * Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side.
42
     *
43
     * @param ArrayCollection $channels
44
     *
45
     * @return ArrayCollection
46
     */
47
    protected function sortChannels(ArrayCollection $channels)
48
    {
49
        $channels->map(function ($channel) {
50
            $channel->setChildren($this->sortChannels($channel->getChildren()));
51
        });
52
53
        $iterator = $channels->getIterator();
54
        $iterator->uasort(function ($a, $b) {
55
            return $a->getName() > $b->getName();
56
        });
57
58
        return new ArrayCollection(iterator_to_array($iterator));
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function createVideo(
65
        $videoManagerId,
66
        $fileName,
67
        $title = '',
68
        $description = '',
69
        $channel = null,
70
        $group = null,
71
        array $keywords = [],
72
        $autoPublish = null
73
    ) {
74
        $response = $this->makeRequest('POST', 'videos', [
75
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
76
            'json' => $this->buildJsonParameters(
77
                compact('fileName'), // Required parameters
78
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
79
            ),
80
        ]);
81
82
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
83
        // wraps headers in arrays.
84
        $videoLocation = is_array($response->getHeader('location'))
85
            ? $response->getHeader('location')[0]
86
            : $response->getHeader('location');
87
88
        $pieces = explode('/', $videoLocation);
89
90
        return end($pieces);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
97
    {
98
        $options = [
99
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
100
        ];
101
102
        if ($parameters) {
103
            $options['query'] = $parameters->getContainer();
104
        }
105
106
        $response = $this->makeRequest('GET', 'videos', $options);
107
        $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']);
108
109
        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 string.

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...
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 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...
116
    {
117
        $options = [
118
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
119
        ];
120
121
        if ($parameters) {
122
            $options['query'] = $parameters->getContainer();
123
        }
124
125
        $response = $this->makeRequest('GET', 'videos', $options);
126
127
        return json_decode($response->getBody()->getContents(), true)['total'];
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getVideoUploadUrl($videoManagerId, $videoId)
134
    {
135
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
136
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
137
        ]);
138
139
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
140
        // wraps headers in arrays.
141
        return is_array($response->getHeader('location'))
142
            ? $response->getHeader('location')[0]
143
            : $response->getHeader('location');
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function updateVideo($videoManagerId, $videoId, $title, $description)
150
    {
151
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
152
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
153
            'json' => $this->buildJsonParameters([], compact('title', 'description')),
154
        ]);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
161
    {
162
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
163
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
164
        ]);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
171
    {
172
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
173
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
174
            'json' => $metadata,
175
        ]);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
182
    {
183
        $url = sprintf(
184
            'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
185
            $videoId,
186
            $playerDefinitionId,
187
            $embedType
188
        );
189
190
        if ($this->cacheTtl) {
191
            $url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl);
192
        }
193
194
        $response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]);
195
196
        $data = \json_decode($response->getBody(), true);
197
        $embedCode = new EmbedCode();
198
        $embedCode->setCode($data['embedCode']);
199
200
        return $embedCode;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function deleteVideo($videoManagerId, $videoId)
207
    {
208
        $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [
209
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
210
        ]);
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 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...
217
    {
218
        $options = [
219
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
220
        ];
221
222
        if ($parameters) {
223
            $options['query'] = $parameters->getContainer();
224
        }
225
226
        $response = $this->makeRequest(
227
            'GET',
228
            sprintf('videos/%s', $videoId),
229
            $options
230
        );
231
232
        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...
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function getAttachments($videoManagerId, $videoId)
239
    {
240
        $response = $this->makeRequest(
241
            'GET',
242
            sprintf('videos/%s/attachments', $videoId),
243
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
244
        );
245
246
        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...
247
    }
248
}
249