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

AbstractHttpEmitter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 6 1
A sendHeaders() 0 10 2
A canSendHeaders() 0 4 2
getContentType() 0 1 ?
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