Passed
Push — 1.x ( e91c3c...4cbda7 )
by Kevin
01:45
created

Response::dump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Behat\Mink\Session;
6
use Symfony\Component\VarDumper\VarDumper;
7
use Zenstruck\Browser\Response\HtmlResponse;
8
use Zenstruck\Browser\Response\JsonResponse;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
class Response
14
{
15
    private Session $session;
16
17
    final public function __construct(Session $session)
18
    {
19
        $this->session = $session;
20
    }
21
22
    final public function statusCode(): int
23
    {
24
        return $this->session->getStatusCode();
25
    }
26
27
    final public static function createFor(Session $session): self
28
    {
29
        $contentType = $session->getResponseHeader('content-type');
30
31
        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

31
        if (str_contains(/** @scrutinizer ignore-type */ $contentType, 'json')) {
Loading history...
32
            return new JsonResponse($session);
33
        }
34
35
        if (str_contains($contentType, 'html')) {
36
            return new HtmlResponse($session);
37
        }
38
39
        return new self($session);
40
    }
41
42
    public function body()
43
    {
44
        return $this->session->getPage()->getContent();
45
    }
46
47
    final public function raw(): string
48
    {
49
        return "{$this->rawMetadata()}\n{$this->rawBody()}";
50
    }
51
52
    final public function isSuccessful(): bool
53
    {
54
        return $this->statusCode() >= 200 && $this->statusCode() < 300;
55
    }
56
57
    final public function isRedirect(): bool
58
    {
59
        return $this->statusCode() >= 300 && $this->statusCode() < 400;
60
    }
61
62
    public function dump(?string $selector = null): void
63
    {
64
        if (null !== $selector) {
65
            throw new \LogicException('$selector cannot be used with this response type.');
66
        }
67
68
        VarDumper::dump($this->raw());
69
    }
70
71
    /**
72
     * @return mixed
73
     */
74
    public function find(string $selector)
75
    {
76
        throw new \LogicException('Find cannot be used on this response type.');
77
    }
78
79
    final protected function session(): Session
80
    {
81
        return $this->session;
82
    }
83
84
    protected function rawMetadata(): string
85
    {
86
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
87
88
        foreach ($this->session->getResponseHeaders() as $header => $values) {
89
            foreach ($values as $value) {
90
                $ret .= "{$header}: {$value}\n";
91
            }
92
        }
93
94
        return $ret;
95
    }
96
97
    protected function rawBody(): string
98
    {
99
        return $this->body();
100
    }
101
}
102