Completed
Push — master ( 5fa477...bba096 )
by Ruben
02:50
created

AbstractApiClient::getCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

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