Completed
Push — master ( fffb97...85dbdb )
by Dmitry
03:32
created

Response::getService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author Dmitry Gladyshev <[email protected]>
4
 * @date 17/08/2016 13:44
5
 */
6
7
namespace Yandex\Direct\Transport;
8
9
use Yandex\Direct\ConfigurableTrait;
10
use Yandex\Direct\Exception\InvalidArgumentException;
11
12
/**
13
 * Class TransportRequest
14
 * @package Yandex\Direct
15
 */
16
class Response implements ResponseInterface
17
{
18
    use ConfigurableTrait;
19
20
    const UNITS_TYPE_DEBIT  = 0;
21
    const UNITS_TYPE_REST   = 1;
22
    const UNITS_TYPE_LIMIT  = 2;
23
24
    /**
25
     * Response constructor.
26
     *
27
     * @param array $responseAttributes
28
     */
29 2
    public function __construct(array $responseAttributes)
30
    {
31 2
        $this->setOptions($responseAttributes);
32 2
    }
33
34
    /**
35
     * @var string
36
     */
37
    protected $service;
38
39
    /**
40
     * @var string
41
     */
42
    protected $method;
43
44
    /**
45
     * @var array
46
     */
47
    protected $units = [null, null, null];
48
49
    /**
50
     * @var string
51
     */
52
    protected $requestId;
53
54
    /**
55
     * @var string
56
     */
57
    protected $body;
58
59
    /**
60
     * @var int
61
     */
62
    protected $code;
63
64
    /**
65
     * @var array
66
     */
67
    protected $headers = [];
68
69
    /**
70
     * @return array
71
     */
72 1
    public function getUnits()
73
    {
74 1
        return $this->units;
75
    }
76
77
    /**
78
     * @param string $rawUnits
79
     * @throws InvalidArgumentException
80
     */
81 2
    public function setUnits($rawUnits)
82
    {
83 2
        $units = explode('/', $rawUnits);
84 2
        if (is_array($units)
85 2
            && count($units) > 0
86 2
        ) {
87 2
            $this->units = $units;
88 2
        }
89 2
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public function getRequestId()
95
    {
96 1
        return $this->requestId;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    public function getUnitsDebit()
103
    {
104 1
        return $this->getUnits()[self::UNITS_TYPE_DEBIT];
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 1
    public function getUnitsRest()
111
    {
112 1
        return $this->getUnits()[self::UNITS_TYPE_REST];
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118 1
    public function getUnitsLimit()
119
    {
120 1
        return $this->getUnits()[self::UNITS_TYPE_LIMIT];
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 2
    public function getBody()
127
    {
128 2
        return $this->body;
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134 1
    public function getHeaders()
135
    {
136 1
        return $this->headers;
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142 1
    public function getService()
143
    {
144 1
        return $this->service;
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function getMethod()
151
    {
152
        return $this->headers;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->headers; (array) is incompatible with the return type declared by the interface Yandex\Direct\Transport\...nseInterface::getMethod 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...
153
    }
154
155
    public function getCode()
156
    {
157
        return $this->code;
158
    }
159
}
160