Completed
Push — master ( e81b92...349f3b )
by Phil
58:34 queued 08:42
created

AbstractStrategy::applyDefaultResponseHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php declare(strict_types=1);
2
3
namespace League\Route\Strategy;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
abstract class AbstractStrategy implements StrategyInterface
8
{
9
    /** @var array */
10
    protected $defaultResponseHeaders = [];
11
12
    /**
13
     * Get current default response headers
14
     *
15
     * @return array
16
     */
17 8
    public function getDefaultResponseHeaders() : array
18
    {
19 8
        return $this->defaultResponseHeaders;
20
    }
21
22
    /**
23
     * Add or replace a default response header
24
     *
25
     * @param string $name
26
     * @param string $value
27
     *
28
     * @return static
29
     */
30 34
    public function addDefaultResponseHeader(string $name, string $value) : AbstractStrategy
31
    {
32 34
        $this->defaultResponseHeaders[strtolower($name)] = $value;
33
34 34
        return $this;
35
    }
36
37
    /**
38
     * Add multiple default response headers
39
     *
40
     * @param array $headers
41
     *
42
     * @return static
43
     */
44 4
    public function addDefaultResponseHeaders(array $headers) : AbstractStrategy
45
    {
46 4
        foreach ($headers as $name => $value) {
47 4
            $this->addDefaultResponseHeader($name, $value);
48
        }
49
50 4
        return $this;
51
    }
52
53
    /**
54
     * Apply default response headers
55
     *
56
     * Headers that already exist on the response will NOT be replaced.
57
     *
58
     * @param \Psr\Http\Message\ResponseInterface $response
59
     *
60
     * @return \Psr\Http\Message\ResponseInterface
61
     */
62 16
    protected function applyDefaultResponseHeaders(ResponseInterface $response) : ResponseInterface
63
    {
64 16
        foreach ($this->defaultResponseHeaders as $name => $value) {
65 10
            if (! $response->hasHeader($name)) {
66 10
                $response = $response->withHeader($name, $value);
67
            }
68
        }
69
70 16
        return $response;
71
    }
72
}
73