Completed
Push — master ( fb348d...7e59e5 )
by Mathew
02:03
created

MondoClient   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 2
cbo 4
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 4 1
A deserializeResponse() 0 8 1
1
<?php
2
3
namespace Thepixeldeveloper\Mondo;
4
5
use GuzzleHttp\ClientInterface;
6
use JMS\Serializer\SerializerInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Class MondoClient
11
 *
12
 * @package Thepixeldeveloper\Mondo
13
 */
14
class MondoClient implements MondoClientInterface
15
{
16
    /**
17
     * @var ClientInterface
18
     */
19
    protected $guzzleClient;
20
21
    /**
22
     * @var SerializerInterface
23
     */
24
    protected $serialiser;
25
26
    /**
27
     * MondoClient constructor.
28
     *
29
     * @param ClientInterface     $guzzleClient
30
     * @param SerializerInterface $serialiser
31
     */
32
    public function __construct(ClientInterface $guzzleClient, SerializerInterface $serialiser)
33
    {
34
        $this->guzzleClient = $guzzleClient;
35
        $this->serialiser = $serialiser;
36
    }
37
38
    /**
39
     * @param string $uri
40
     * @param array  $options
41
     *
42
     * @return \Psr\Http\Message\ResponseInterface
43
     */
44
    public function get($uri, $options = [])
45
    {
46
        return $this->guzzleClient->request('GET', $uri, $options);
47
    }
48
49
    /**
50
     * @param ResponseInterface $data
51
     * @param string            $type
52
     *
53
     * @return array|\JMS\Serializer\scalar|object
54
     */
55
    public function deserializeResponse(ResponseInterface $data, $type)
56
    {
57
        return $this->serialiser->deserialize(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serialiser...ents(), $type, 'json'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface Thepixeldeveloper\Mondo\...ce::deserializeResponse of type object.

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...
58
            $data->getBody()->getContents(),
59
            $type,
60
            'json'
61
        );
62
    }
63
}
64