Completed
Pull Request — master (#8)
by Omid
25:19
created

AbstractApiClient::createVideo()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace MovingImage\Client\VMPro\ApiClient;
4
5
use MovingImage\Client\VMPro\Entity\Channel;
6
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
7
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
8
use MovingImage\Util\Logging\Traits\LoggerAwareTrait;
9
10
/**
11
 * Class AbstractApiClient.
12
 *
13
 * @author Ruben Knol <[email protected]>
14
 * @author Omid Rad <[email protected]>
15
 */
16
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
17
{
18
    use LoggerAwareTrait;
19
20
    /**
21 4
     * {@inheritdoc}
22
     */
23 4
    public function getChannels($videoManagerId)
24 4
    {
25 4
        $response = $this->makeRequest('GET', 'channels', [
26
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
27 4
        ]);
28
29
        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...
30
    }
31
32
    /**
33 10
     * {@inheritdoc}
34
     */
35
    public function createVideo(
36
        $videoManagerId,
37
        $fileName,
38
        $title = '',
39
        $description = '',
40
        $channel = null,
41
        $group = null,
42
        array $keywords = [],
43 10
        $autoPublish = null
44 10
    ) {
45 10
        $response = $this->makeRequest('POST', 'videos', [
46 10
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
47 10
            'json' => $this->buildJsonParameters(
48 10
                compact('fileName'), // Required parameters
49 8
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
50
            ),
51
        ]);
52
53 6
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
54 6
        // wraps headers in arrays.
55 6
        $videoLocation = is_array($response->getHeader('location'))
56
            ? $response->getHeader('location')[0]
57 6
            : $response->getHeader('location');
58
59 6
        $pieces = explode('/', $videoLocation);
60
61
        return end($pieces);
62
    }
63
64
    /**
65 4
     * {@inheritdoc}
66
     */
67 4
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
68 4
    {
69 4
        if ($parameters) {
70
            $parameters = $parameters->getContainer();
71
            $parameters[self::OPT_VIDEO_MANAGER_ID] = $videoManagerId;
72
        } else {
73 2
            $parameters = [];
74 2
        }
75 2
76
        $response = $this->makeRequest('GET', 'videos', $parameters);
77
78
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
79
        // wraps headers in arrays.
80
        return is_array($response->getHeader('location'))
81
            ? $response->getHeader('location')[0]
82
            : $response->getHeader('location');
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getVideoUploadUrl($videoManagerId, $videoId)
89
    {
90
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
91
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
92
        ]);
93
94
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
95
        // wraps headers in arrays.
96
        return is_array($response->getHeader('location'))
97
            ? $response->getHeader('location')[0]
98
            : $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