| Total Complexity | 6 |
| Total Lines | 71 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 3 | class FileOutputWriter implements \DaveRandom\Resume\OutputWriter |
||
| 4 | { |
||
| 5 | /** |
||
| 6 | * @var string |
||
| 7 | */ |
||
| 8 | private $file; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var bool |
||
| 12 | */ |
||
| 13 | private $headerWritten = false; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | private $responseCode; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string[] |
||
| 22 | */ |
||
| 23 | private $headers = []; |
||
| 24 | |||
| 25 | private function writeHeader() |
||
| 26 | { |
||
| 27 | $header = "HTTP/1.1 {$this->responseCode} " . self::RESPONSE_MESSAGES[$this->responseCode] . "\r\n" |
||
| 28 | . \implode("\r\n", $this->headers) . "\r\n" |
||
| 29 | . "\r\n"; |
||
| 30 | |||
| 31 | \file_put_contents($this->file, $header); |
||
| 32 | |||
| 33 | $this->headerWritten = true; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function __construct(string $file) |
||
| 37 | { |
||
| 38 | $this->file = $file; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Set the HTTP response code to send to the client |
||
| 43 | * |
||
| 44 | * @param int $code |
||
| 45 | */ |
||
| 46 | function setResponseCode(int $code): void |
||
| 47 | { |
||
| 48 | $this->responseCode = $code; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Send a response header to the client |
||
| 53 | * |
||
| 54 | * @param string $name |
||
| 55 | * @param string $value |
||
| 56 | */ |
||
| 57 | function sendHeader(string $name, string $value): void |
||
| 58 | { |
||
| 59 | $this->headers[] = "{$name}: {$value}"; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Send a data block to the client |
||
| 64 | * |
||
| 65 | * @param string $data |
||
| 66 | */ |
||
| 67 | function sendData(string $data): void |
||
| 68 | { |
||
| 69 | if (!$this->headerWritten) { |
||
| 70 | $this->writeHeader(); |
||
| 71 | } |
||
| 72 | |||
| 73 | \file_put_contents($this->file, $data, \FILE_APPEND); |
||
| 74 | } |
||
| 76 |