Response   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 96
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDuration() 0 4 1
A getStatusCode() 0 4 1
A isModified() 0 4 2
A getBody() 0 8 2
A getLastModified() 0 10 3
A getHeaders() 0 4 1
A getHeader() 0 4 1
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();
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...
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);
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...
113
    }
114
}
115