Completed
Pull Request — master (#319)
by Alex
16:11 queued 06:09
created

Response::isModified()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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
     * @param \Psr\Http\Message\ResponseInterface
35
     */
36 4
    public function __construct(PsrResponseInterface $psrResponse)
37
    {
38 4
        $this->psrResponse = $psrResponse;
39 4
    }
40
41
    /**
42
     * @return int
43
     */
44 2
    public function getStatusCode(): int
45
    {
46 2
        return (int) $this->psrResponse->getStatusCode();
47
    }
48
49
    /**
50
     * @return boolean
51
     */
52 3
    public function isModified() : bool
53
    {
54 3
        return $this->psrResponse->getStatusCode() != 304 && strlen($this->getBody()) > 0;
55 3
    }
56
57
    /**
58 3
     * @return string
59
     */
60
    public function getBody() : ? string
61
    {
62
        if (is_null($this->body)) {
63
            $this->body = $this->psrResponse->getBody()->getContents();
64 1
        }
65
66 1
        return $this->body;
67 1
    }
68
69 1
    /**
70
     * @return \DateTime|null
71
     */
72
    public function getLastModified() : ?\DateTime
73
    {
74
        if ($this->psrResponse->hasHeader(static::HTTP_LAST_MODIFIED)) {
75
            $lastModified = \DateTime::createFromFormat(\DateTime::RFC2822, $this->getHeader(static::HTTP_LAST_MODIFIED)[0]);
76
77
            return false === $lastModified ? null : $lastModified;
78 1
        }
79
80 1
        return null;
81
    }
82
83
    /**
84
     * @return iterable
85
     */
86
    public function getHeaders()  : iterable
87 1
    {
88
        return $this->psrResponse->getHeaders();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->psrResponse->getHeaders(); (string[][]) is incompatible with the return type declared by the interface FeedIo\Adapter\ResponseInterface::getHeaders of type FeedIo\Adapter\iterable.

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...
89 1
    }
90
91
    /**
92
     * @param  string       $name
93
     * @return iterable
94
     */
95
    public function getHeader(string $name) : iterable
96
    {
97
        return $this->psrResponse->getHeader($name);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->psrResponse->getHeader($name); (string[]) is incompatible with the return type declared by the interface FeedIo\Adapter\ResponseInterface::getHeader of type FeedIo\Adapter\iterable.

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...
98
    }
99
}
100