Passed
Push — master ( 8da5a8...bfab6a )
by
unknown
02:54
created

AbstractApiClient::sortChannels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\Entity\Channel;
7
use MovingImage\Client\VMPro\Entity\EmbedCode;
8
use MovingImage\Client\VMPro\Entity\Video;
9
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
10
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
11
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
12
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait;
13
14
/**
15
 * Class AbstractApiClient.
16
 *
17
 * @author Ruben Knol <[email protected]>
18
 * @author Omid Rad <[email protected]>
19
 */
20
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
21
{
22
    use LoggerAwareTrait;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getChannels($videoManagerId)
28
    {
29
        $response = $this->makeRequest('GET', 'channels', [
30
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
31
        ]);
32
33
        $rootChannel = $this->deserialize($response->getBody(), Channel::class);
34
        $rootChannel->setChildren($this->sortChannels($rootChannel->getChildren()));
35
36
        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...
37
    }
38
39
    /**
40
     * Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side.
41
     *
42
     * @param ArrayCollection $channels
43
     *
44
     * @return ArrayCollection
45
     */
46
    protected function sortChannels(ArrayCollection $channels)
47
    {
48
        $channels->map(function ($channel) {
49
            $channel->setChildren($this->sortChannels($channel->getChildren()));
50
        });
51
52
        $iterator = $channels->getIterator();
53
        $iterator->uasort(function ($a, $b) {
54
            return $a->getName() > $b->getName();
55
        });
56
57
        return new ArrayCollection(iterator_to_array($iterator));
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function createVideo(
64
        $videoManagerId,
65
        $fileName,
66
        $title = '',
67
        $description = '',
68
        $channel = null,
69
        $group = null,
70
        array $keywords = [],
71
        $autoPublish = null
72
    ) {
73
        $response = $this->makeRequest('POST', 'videos', [
74
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
75
            'json' => $this->buildJsonParameters(
76
                compact('fileName'), // Required parameters
77
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
78
            ),
79
        ]);
80
81
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
82
        // wraps headers in arrays.
83
        $videoLocation = is_array($response->getHeader('location'))
84
            ? $response->getHeader('location')[0]
85
            : $response->getHeader('location');
86
87
        $pieces = explode('/', $videoLocation);
88
89
        return end($pieces);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
96
    {
97
        $options = [
98
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
99
        ];
100
101
        if ($parameters) {
102
            $options['query'] = $parameters->getContainer();
103
        }
104
105
        $response = $this->makeRequest('GET', 'videos', $options);
106
        $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']);
107
108
        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...
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 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...
115
    {
116
        $options = [
117
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
118
        ];
119
120
        if ($parameters) {
121
            $options['query'] = $parameters->getContainer();
122
        }
123
124
        $response = $this->makeRequest('GET', 'videos', $options);
125
126
        return json_decode($response->getBody()->getContents(), true)['total'];
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getVideoUploadUrl($videoManagerId, $videoId)
133
    {
134
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
135
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
136
        ]);
137
138
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
139
        // wraps headers in arrays.
140
        return is_array($response->getHeader('location'))
141
            ? $response->getHeader('location')[0]
142
            : $response->getHeader('location');
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function updateVideo($videoManagerId, $videoId, $title, $description)
149
    {
150
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
151
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
152
            'json' => $this->buildJsonParameters([], compact('title', 'description')),
153
        ]);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
160
    {
161
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
162
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
163
        ]);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
170
    {
171
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
172
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
173
            'json' => $metadata,
174
        ]);
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
181
    {
182
        $response = $this->makeRequest('GET',
183
            sprintf('videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
184
                $videoId, $playerDefinitionId, $embedType), [
185
                self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
186
            ]
187
        );
188
189
        $data = \json_decode($response->getBody(), true);
190
        $embedCode = new EmbedCode();
191
        $embedCode->setCode($data['embedCode']);
192
193
        return $embedCode;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function deleteVideo($videoManagerId, $videoId)
200
    {
201
        $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [
202
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
203
        ]);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209 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...
210
    {
211
        $options = [
212
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
213
        ];
214
215
        if ($parameters) {
216
            $options['query'] = $parameters->getContainer();
217
        }
218
219
        $response = $this->makeRequest(
220
            'GET',
221
            sprintf('videos/%s', $videoId),
222
            $options
223
        );
224
225
        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...
226
    }
227
}
228