|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the feed-io package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace FeedIo\Adapter\Guzzle; |
|
12
|
|
|
|
|
13
|
|
|
use FeedIo\Adapter\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Guzzle dependent HTTP Response |
|
18
|
|
|
*/ |
|
19
|
|
|
class Response implements ResponseInterface |
|
20
|
|
|
{ |
|
21
|
|
|
const HTTP_LAST_MODIFIED = 'Last-Modified'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Psr\Http\Message\ResponseInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $psrResponse; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $body; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
4 |
|
protected $duration; |
|
37
|
|
|
|
|
38
|
4 |
|
/** |
|
39
|
4 |
|
* @param PsrResponseInterface $psrResponse |
|
40
|
|
|
* @param int $duration |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(PsrResponseInterface $psrResponse, int $duration) |
|
43
|
|
|
{ |
|
44
|
2 |
|
$this->psrResponse = $psrResponse; |
|
45
|
|
|
$this->duration = $duration; |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return int |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getDuration(): int |
|
52
|
3 |
|
{ |
|
53
|
|
|
return $this->duration; |
|
54
|
3 |
|
} |
|
55
|
3 |
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return int |
|
58
|
3 |
|
*/ |
|
59
|
|
|
public function getStatusCode(): int |
|
60
|
|
|
{ |
|
61
|
|
|
return (int) $this->psrResponse->getStatusCode(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
/** |
|
65
|
|
|
* @return boolean |
|
66
|
1 |
|
*/ |
|
67
|
1 |
|
public function isModified() : bool |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->psrResponse->getStatusCode() != 304 && strlen($this->getBody()) > 0; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getBody() : ? string |
|
76
|
|
|
{ |
|
77
|
|
|
if (is_null($this->body)) { |
|
78
|
1 |
|
$this->body = $this->psrResponse->getBody()->getContents(); |
|
79
|
|
|
} |
|
80
|
1 |
|
|
|
81
|
|
|
return $this->body; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \DateTime|null |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getLastModified() : ?\DateTime |
|
88
|
|
|
{ |
|
89
|
1 |
|
if ($this->psrResponse->hasHeader(static::HTTP_LAST_MODIFIED)) { |
|
90
|
|
|
$lastModified = \DateTime::createFromFormat(\DateTime::RFC2822, $this->getHeader(static::HTTP_LAST_MODIFIED)[0]); |
|
91
|
|
|
|
|
92
|
|
|
return false === $lastModified ? null : $lastModified; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return iterable |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getHeaders() : iterable |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->psrResponse->getHeaders(); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
* @return iterable |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getHeader(string $name) : iterable |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->psrResponse->getHeader($name); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.