Completed
Push — master ( f2e227...0d4270 )
by Ruben
02:34
created

Guzzle5ApiClient::_doRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace MovingImage\Client\VMPro\ApiClient;
4
5
use GuzzleHttp\Message\Response;
6
use MovingImage\Client\VMPro\ApiClient;
7
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
8
use GuzzleHttp\Message\ResponseInterface;
9
use MovingImage\Client\VMPro\Exception;
10
use GuzzleHttp\Stream\Stream;
11
12
/**
13
 * Class Guzzle5ApiClient.
14
 *
15
 * @author Ruben Knol <[email protected]>
16
 */
17
class Guzzle5ApiClient extends ApiClient implements ApiClientInterface
18
{
19
    /**
20
     * Guzzle5 Client implementation for making HTTP requests with
21
     * the appropriate options.
22
     *
23
     * @param string $method
24
     * @param string $uri
25
     * @param array  $options
26
     *
27
     * @return \GuzzleHttp\Message\ResponseInterface
28
     */
29
    protected function _doRequest($method, $uri, $options)
30
    {
31
        // For Guzzle5 we cannot have any options that are not pre-defined,
32
        // so instead we put it in the config array
33
        $options['config'][self::OPT_VIDEO_MANAGER_ID] = $options[self::OPT_VIDEO_MANAGER_ID];
34
        unset($options[self::OPT_VIDEO_MANAGER_ID]);
35
36
        $request = $this->httpClient->createRequest($method, $uri, $options);
0 ignored issues
show
Bug introduced by
The method createRequest() does not exist on GuzzleHttp\ClientInterface. Did you maybe mean request()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37
38
        return $this->httpClient->send($request);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @param ResponseInterface $response
45
     *
46
     * @return string
47
     */
48 View Code Duplication
    protected function serializeResponse($response)
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...
49
    {
50
        /** @var ResponseInterface $response */
51
        $serialized = serialize([
52
            $response->getStatusCode(),
53
            $response->getHeaders(),
54
            $response->getBody()->getContents(),
55
        ]);
56
57
        //subsequent calls need to access the stream from the beginning
58
        $response->getBody()->seek(0);
59
60
        return $serialized;
61
    }
62
63
    /**
64
     * Unserializes the serialized response into a ResponseInterface instance.
65
     *
66
     * @param string $serialized
67
     *
68
     * @return ResponseInterface
69
     *
70
     * @throws Exception
71
     */
72
    protected function unserializeResponse($serialized)
73
    {
74
        $array = unserialize($serialized);
75
        if (!is_array($array) || count($array) !== 3) {
76
            throw new Exception(sprintf('Error unserializing response: %s', $serialized));
77
        }
78
79
        return new Response($array[0], $array[1], Stream::factory($array[2]));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \GuzzleHttp\M...m::factory($array[2])); (GuzzleHttp\Message\Response) is incompatible with the return type of the parent method MovingImage\Client\VMPro...nt::unserializeResponse of type Psr\Http\Message\ResponseInterface.

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...
80
    }
81
}
82