Completed
Push — master ( d4a365...c9acbc )
by
unknown
19:08 queued 13:13
created

AbstractApiClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 164
ccs 25
cts 75
cp 0.3333
rs 10
c 0
b 0
f 0

10 Methods

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