Completed
Pull Request — master (#15)
by Nikita
24:53
created

AbstractApiClient::getVideoDownloadUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 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\VideoDownloadUrl;
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 4
     */
25
    public function getChannels($videoManagerId)
26 4
    {
27 4
        $response = $this->makeRequest('GET', 'channels', [
28 4
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
29
        ]);
30 4
31
        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 10
     */
37
    public function createVideo(
38
        $videoManagerId,
39
        $fileName,
40
        $title = '',
41
        $description = '',
42
        $channel = null,
43
        $group = null,
44
        array $keywords = [],
45
        $autoPublish = null
46 10
    ) {
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 8
            ),
53
        ]);
54
55
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
56 6
        // wraps headers in arrays.
57 6
        $videoLocation = is_array($response->getHeader('location'))
58 6
            ? $response->getHeader('location')[0]
59
            : $response->getHeader('location');
60 6
61
        $pieces = explode('/', $videoLocation);
62 6
63
        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 4
     */
88
    public function getVideoUploadUrl($videoManagerId, $videoId)
89 4
    {
90 4
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
91 4
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
92
        ]);
93
94
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
95 2
        // wraps headers in arrays.
96 2
        return is_array($response->getHeader('location'))
97 2
            ? $response->getHeader('location')[0]
98
            : $response->getHeader('location');
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getVideoDownloadUrls($videoManagerId, $videoId)
105
    {
106
        $response = $this->makeRequest('GET', sprintf('videos/%s/download-urls', $videoId), [
107
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
108
        ]);
109
        $response = $response->getBody()->getContents();
110
111
        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...
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function updateVideo($videoManagerId, $videoId, $title, $description)
118
    {
119
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
120
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
121
            'json' => $this->buildJsonParameters([], compact('title', 'description')),
122
        ]);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
129
    {
130
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
131
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
132
        ]);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
139
    {
140
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
141
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
142
            'json' => $metadata,
143
        ]);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
150
    {
151
        $response = $this->makeRequest('GET',
152
            sprintf('videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
153
                $videoId, $playerDefinitionId, $embedType), [
154
                self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
155
            ]
156
        );
157
158
        $data = \json_decode($response->getBody(), true);
159
160
        return $data['embedCode'];
161
    }
162
}
163