Completed
Pull Request — master (#5)
by Ruben
06:28
created

AbstractApiClient::createVideo()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 13
cts 13
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\Interfaces\ApiClientInterface;
7
use MovingImage\Util\Logging\Traits\LoggerAwareTrait;
8
9
/**
10
 * Class AbstractApiClient.
11
 *
12
 * @author Ruben Knol <[email protected]>
13
 */
14
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
15
{
16
    use LoggerAwareTrait;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 12
    public function getChannels($videoManagerId)
22
    {
23 12
        $response = $this->makeRequest('GET', '%videoManagerId%/channels', [
24 12
            'videoManagerId' => $videoManagerId,
25 8
        ]);
26
27 12
        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...
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 30
    public function createVideo(
34
        $videoManagerId,
35
        $fileName,
36
        $title = '',
37
        $description = '',
38
        $channel = null,
39
        $group = null,
40
        array $keywords = [],
41
        $autoPublish = null
42
    ) {
43 30
        $response = $this->makeRequest('POST', '%videoManagerId%/videos', [
44 30
            'videoManagerId' => $videoManagerId,
45 30
            'json' => $this->buildJsonParameters(
46 30
                compact('fileName'), // Required parameters
47 30
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
48 20
            ),
49 16
        ]);
50
51
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
52
        // wraps headers in arrays.
53 18
        $videoLocation = is_array($response->getHeader('location'))
54 15
            ? $response->getHeader('location')[0]
55 18
            : $response->getHeader('location');
56
57 18
        $pieces = explode('/', $videoLocation);
58
59 18
        return $pieces[count($pieces) - 1];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 12
    public function getVideoUploadUrl($videoManagerId, $videoId)
66
    {
67 12
        $response = $this->makeRequest('GET', sprintf('%%videoManagerId%%/videos/%s/url', $videoId), [
68 12
            'videoManagerId' => $videoManagerId,
69 8
        ]);
70
71
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
72
        // wraps headers in arrays.
73 6
        return is_array($response->getHeader('location'))
74 5
            ? $response->getHeader('location')[0]
75 6
            : $response->getHeader('location');
76
    }
77
}
78