Passed
Push — 1.x ( a604cc...a74d24 )
by Kevin
01:42
created

Response::createFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 1
b 0
f 1
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
abstract 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
        throw new \RuntimeException("Unable to create browser response for \"{$contentType}\".");
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
    abstract public function find(string $selector);
65
66
    final protected function session(): Session
67
    {
68
        return $this->session;
69
    }
70
71
    protected function rawMetadata(): string
72
    {
73
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
74
75
        foreach ($this->session->getResponseHeaders() as $header => $values) {
76
            foreach ($values as $value) {
77
                $ret .= "{$header}: {$value}\n";
78
            }
79
        }
80
81
        return $ret;
82
    }
83
84
    protected function rawBody(): string
85
    {
86
        return $this->body();
87
    }
88
}
89