| Total Complexity | 11 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 12 | abstract class Response |
||
| 13 | { |
||
| 14 | private Session $session; |
||
| 15 | |||
| 16 | final public function __construct(Session $session) |
||
| 19 | } |
||
| 20 | |||
| 21 | final public static function createFor(Session $session): self |
||
| 22 | { |
||
| 23 | $contentType = $session->getResponseHeader('content-type'); |
||
| 24 | |||
| 25 | if (str_contains($contentType, 'json')) { |
||
|
|
|||
| 26 | return new JsonResponse($session); |
||
| 27 | } |
||
| 28 | |||
| 29 | if (str_contains($contentType, 'html')) { |
||
| 30 | return new HtmlResponse($session); |
||
| 31 | } |
||
| 32 | |||
| 33 | throw new \RuntimeException("Unable to create browser response for \"{$contentType}\"."); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function body() |
||
| 37 | { |
||
| 38 | return $this->session->getPage()->getContent(); |
||
| 39 | } |
||
| 40 | |||
| 41 | final public function raw(): string |
||
| 42 | { |
||
| 43 | return "{$this->rawMetadata()}\n{$this->rawBody()}"; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | abstract public function find(string $selector); |
||
| 50 | |||
| 51 | final protected function session(): Session |
||
| 54 | } |
||
| 55 | |||
| 56 | protected function rawMetadata(): string |
||
| 57 | { |
||
| 58 | $ret = "URL: {$this->session->getCurrentUrl()} ({$this->session->getStatusCode()})\n\n"; |
||
| 59 | |||
| 60 | foreach ($this->session->getResponseHeaders() as $header => $values) { |
||
| 61 | foreach ($values as $value) { |
||
| 62 | $ret .= "{$header}: {$value}\n"; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return $ret; |
||
| 67 | } |
||
| 68 | |||
| 69 | protected function rawBody(): string |
||
| 72 | } |
||
| 73 | } |
||
| 74 |