Completed
Push — master ( b96870...b9bbf6 )
by Dusan
11s
created

AbstractHttpEmitter::sendHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WeCodeIn\ErrorHandling\Emitter;
6
7
use Throwable;
8
9
abstract class AbstractHttpEmitter extends AbstractEmitter
10
{
11
    /**
12
     * @var int
13
     */
14
    protected $httpResponseCode;
15
16 3
    public function __construct(bool $includeTrace = true, int $httpResponseCode = 500)
17
    {
18 3
        parent::__construct($includeTrace);
19
20 3
        $this->httpResponseCode = $httpResponseCode;
21 3
    }
22
23 3
    public function __invoke(Throwable $throwable) : Throwable
24
    {
25 3
        $this->sendHeaders();
26
27 3
        return parent::__invoke($throwable);
28
    }
29
30 3
    protected function sendHeaders()
31
    {
32 3
        if (!$this->canSendHeaders()) {
33 1
            return;
34
        }
35
36 2
        http_response_code($this->httpResponseCode);
37
38 2
        header("Content-Type: {$this->getContentType()}");
39 2
    }
40
41 3
    final protected function canSendHeaders() : bool
42
    {
43 3
        return !headers_sent() && isset($_SERVER["REQUEST_URI"]);
44
    }
45
46
    abstract protected function getContentType() : string;
47
}
48