| Total Complexity | 11 |
| Total Lines | 131 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | class Response implements IResponse |
||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * response content |
||
| 13 | * |
||
| 14 | * @var mixed |
||
| 15 | */ |
||
| 16 | protected $content; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * http status code |
||
| 20 | * |
||
| 21 | * @var Integer |
||
| 22 | */ |
||
| 23 | protected $code; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * header manager |
||
| 27 | * |
||
| 28 | * @var Headers |
||
| 29 | */ |
||
| 30 | protected $headerManager; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * is cli |
||
| 34 | * |
||
| 35 | * @var Boolean |
||
| 36 | */ |
||
| 37 | protected $isCli; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * instanciate |
||
| 41 | */ |
||
| 42 | 9 | public function __construct() |
|
| 43 | { |
||
| 44 | 9 | $this->headerManager = new Headers(); |
|
| 45 | 9 | $this->code = self::HTTP_NOT_FOUND; |
|
| 46 | 9 | $this->content = ''; |
|
| 47 | 9 | $sapiName = php_sapi_name(); |
|
| 48 | 9 | $this->setIsCli($sapiName == self::_CLI || $sapiName == self::_CLID); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * returns header manager |
||
| 53 | * |
||
| 54 | * @return Headers |
||
| 55 | */ |
||
| 56 | 1 | public function getHeaderManager(): Headers |
|
| 57 | { |
||
| 58 | 1 | return $this->headerManager; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * set response content |
||
| 63 | * |
||
| 64 | * @param mixed $content |
||
| 65 | * @return Response |
||
| 66 | */ |
||
| 67 | 1 | public function setContent($content): Response |
|
| 68 | { |
||
| 69 | 1 | $this->content = (is_string($content)) |
|
| 70 | 1 | ? $content |
|
| 71 | 1 | : json_encode($content); |
|
| 72 | 1 | $this->headerManager->add( |
|
| 73 | 1 | Headers::CONTENT_LENGTH, |
|
| 74 | 1 | (string) strlen($this->content) |
|
| 75 | ); |
||
| 76 | 1 | return $this; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * return content string |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | 1 | public function getContent(): string |
|
| 85 | { |
||
| 86 | 1 | return $this->content; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * set http code response |
||
| 91 | * |
||
| 92 | * @param integer $code |
||
| 93 | * @return Response |
||
| 94 | */ |
||
| 95 | 1 | public function setCode(int $code): Response |
|
| 96 | { |
||
| 97 | 1 | $this->code = $code; |
|
| 98 | 1 | return $this; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * return http code response |
||
| 103 | * |
||
| 104 | * @return integer |
||
| 105 | */ |
||
| 106 | 1 | public function getCode(): int |
|
| 107 | { |
||
| 108 | 1 | return $this->code; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * send response content to output |
||
| 113 | * |
||
| 114 | * @return Response |
||
| 115 | */ |
||
| 116 | 1 | public function send(): Response |
|
| 117 | { |
||
| 118 | 1 | if ($this->isCli) { |
|
| 119 | 1 | echo $this->content; |
|
| 120 | 1 | return $this; |
|
| 121 | } |
||
| 122 | 1 | $this->headerManager->send(); |
|
| 123 | 1 | http_response_code($this->code); |
|
| 124 | 1 | echo $this->content; |
|
| 125 | 1 | return $this; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * set true if we are running from cli |
||
| 130 | * essentially for testing purposes |
||
| 131 | * |
||
| 132 | * @param boolean $isCli |
||
| 133 | * @return Response |
||
| 134 | */ |
||
| 135 | 1 | protected function setIsCli(bool $isCli): Response |
|
| 139 | } |
||
| 140 | } |
||
| 141 |