| Total Complexity | 11 |
| Total Lines | 131 |
| Duplicated Lines | 0 % |
| 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 | public function __construct() |
||
| 43 | { |
||
| 44 | $this->headerManager = new Headers(); |
||
| 45 | $this->code = self::HTTP_NOT_FOUND; |
||
| 46 | $this->content = ''; |
||
| 47 | $sapiName = php_sapi_name(); |
||
| 48 | $this->setIsCli($sapiName == self::_CLI || $sapiName == self::_CLID); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * returns header manager |
||
| 53 | * |
||
| 54 | * @return Headers |
||
| 55 | */ |
||
| 56 | public function getHeaderManager(): Headers |
||
| 57 | { |
||
| 58 | return $this->headerManager; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * set response content |
||
| 63 | * |
||
| 64 | * @param mixed $content |
||
| 65 | * @return Response |
||
| 66 | */ |
||
| 67 | public function setContent($content): Response |
||
| 68 | { |
||
| 69 | $this->content = (is_string($content)) |
||
| 70 | ? $content |
||
| 71 | : json_encode($content); |
||
| 72 | $this->headerManager->add( |
||
| 73 | Headers::CONTENT_LENGTH, |
||
| 74 | (string) strlen($this->content) |
||
| 75 | ); |
||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * return content string |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getContent(): string |
||
| 85 | { |
||
| 86 | return $this->content; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * set http code response |
||
| 91 | * |
||
| 92 | * @param integer $code |
||
| 93 | * @return Response |
||
| 94 | */ |
||
| 95 | public function setCode(int $code): Response |
||
| 96 | { |
||
| 97 | $this->code = $code; |
||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * return http code response |
||
| 103 | * |
||
| 104 | * @return integer |
||
| 105 | */ |
||
| 106 | public function getCode(): int |
||
| 107 | { |
||
| 108 | return $this->code; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * send response content to output |
||
| 113 | * |
||
| 114 | * @return Response |
||
| 115 | */ |
||
| 116 | public function send(): Response |
||
| 117 | { |
||
| 118 | if ($this->isCli) { |
||
| 119 | echo $this->content; |
||
| 120 | return $this; |
||
| 121 | } |
||
| 122 | $this->headerManager->send(); |
||
| 123 | http_response_code($this->code); |
||
| 124 | echo $this->content; |
||
| 125 | 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 | protected function setIsCli(bool $isCli): Response |
||
| 139 | } |
||
| 140 | } |
||
| 141 |