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

AbstractStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 66
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultResponseHeaders() 0 4 1
A addDefaultResponseHeader() 0 6 1
A addDefaultResponseHeaders() 0 8 2
A applyDefaultResponseHeaders() 0 10 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