Passed
Pull Request — 1.x (#28)
by Kevin
02:58 queued 48s
created

Response::crawler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Behat\Mink\Session;
6
use Symfony\Component\DomCrawler\Crawler;
7
use Symfony\Component\VarDumper\VarDumper;
8
use Zenstruck\Browser\Response\HtmlResponse;
9
use Zenstruck\Browser\Response\JsonResponse;
10
use Zenstruck\Browser\Response\XmlResponse;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 *
15
 * @internal
16
 */
17
class Response
18
{
19
    private Session $session;
20
21
    final public function __construct(Session $session)
22
    {
23
        $this->session = $session;
24
    }
25
26
    final public function statusCode(): int
27
    {
28
        return $this->session->getStatusCode();
29
    }
30
31
    final public static function createFor(Session $session): self
32
    {
33
        $contentType = (string) $session->getResponseHeader('content-type');
34
35
        if (str_contains($contentType, 'json')) {
36
            return new JsonResponse($session);
37
        }
38
39
        if (str_contains($contentType, 'html')) {
40
            return new HtmlResponse($session);
41
        }
42
43
        if (str_contains($contentType, 'xml')) {
44
            return new XmlResponse($session);
45
        }
46
47
        return new self($session);
48
    }
49
50
    final public function body(): string
51
    {
52
        return $this->session->getPage()->getContent();
53
    }
54
55
    final public function raw(): string
56
    {
57
        return "{$this->rawMetadata()}\n{$this->rawBody()}";
58
    }
59
60
    final public function isSuccessful(): bool
61
    {
62
        return $this->statusCode() >= 200 && $this->statusCode() < 300;
63
    }
64
65
    final public function isRedirect(): bool
66
    {
67
        return $this->statusCode() >= 300 && $this->statusCode() < 400;
68
    }
69
70
    public function crawler(): Crawler
71
    {
72
        throw new \LogicException('Crawler is not available for this response type.');
73
    }
74
75
    public function dump(?string $selector = null): void
76
    {
77
        if (null !== $selector) {
78
            throw new \LogicException('$selector cannot be used with this response type.');
79
        }
80
81
        VarDumper::dump($this->raw());
82
    }
83
84
    final protected function session(): Session
85
    {
86
        return $this->session;
87
    }
88
89
    protected function rawMetadata(): string
90
    {
91
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
92
93
        foreach ($this->session->getResponseHeaders() as $header => $values) {
94
            foreach ($values as $value) {
95
                $ret .= "{$header}: {$value}\n";
96
            }
97
        }
98
99
        return $ret;
100
    }
101
102
    protected function rawBody(): string
103
    {
104
        return $this->body();
105
    }
106
}
107