Passed
Push — 1.x ( a74d24...5e9553 )
by Kevin
01:49
created

Response   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 22
c 2
b 0
f 2
dl 0
loc 78
rs 10
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A statusCode() 0 3 1
A isRedirect() 0 3 2
A raw() 0 3 1
A isSuccessful() 0 3 2
A createFor() 0 13 3
A body() 0 3 1
A session() 0 3 1
A find() 0 3 1
A rawBody() 0 3 1
A rawMetadata() 0 11 3
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Behat\Mink\Session;
6
use Zenstruck\Browser\Response\HtmlResponse;
7
use Zenstruck\Browser\Response\JsonResponse;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
class Response
13
{
14
    private Session $session;
15
16
    final public function __construct(Session $session)
17
    {
18
        $this->session = $session;
19
    }
20
21
    final public function statusCode(): int
22
    {
23
        return $this->session->getStatusCode();
24
    }
25
26
    final public static function createFor(Session $session): self
27
    {
28
        $contentType = $session->getResponseHeader('content-type');
29
30
        if (str_contains($contentType, 'json')) {
0 ignored issues
show
Bug introduced by
It seems like $contentType can also be of type null; however, parameter $haystack of str_contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        if (str_contains(/** @scrutinizer ignore-type */ $contentType, 'json')) {
Loading history...
31
            return new JsonResponse($session);
32
        }
33
34
        if (str_contains($contentType, 'html')) {
35
            return new HtmlResponse($session);
36
        }
37
38
        return new self($session);
39
    }
40
41
    public function body()
42
    {
43
        return $this->session->getPage()->getContent();
44
    }
45
46
    final public function raw(): string
47
    {
48
        return "{$this->rawMetadata()}\n{$this->rawBody()}";
49
    }
50
51
    final public function isSuccessful(): bool
52
    {
53
        return $this->statusCode() >= 200 && $this->statusCode() < 300;
54
    }
55
56
    final public function isRedirect(): bool
57
    {
58
        return $this->statusCode() >= 300 && $this->statusCode() < 400;
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function find(string $selector)
0 ignored issues
show
Unused Code introduced by
The parameter $selector is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
    public function find(/** @scrutinizer ignore-unused */ string $selector)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        throw new \LogicException('Find cannot be used on this response type.');
67
    }
68
69
    final protected function session(): Session
70
    {
71
        return $this->session;
72
    }
73
74
    protected function rawMetadata(): string
75
    {
76
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
77
78
        foreach ($this->session->getResponseHeaders() as $header => $values) {
79
            foreach ($values as $value) {
80
                $ret .= "{$header}: {$value}\n";
81
            }
82
        }
83
84
        return $ret;
85
    }
86
87
    protected function rawBody(): string
88
    {
89
        return $this->body();
90
    }
91
}
92