Completed
Push — master ( 6a6e14...e9047f )
by Julián
02:21
created

Header::isExcluded()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 8.2222
cc 7
eloc 8
nc 4
nop 1
1
<?php
2
/**
3
 * Effortless maintenance management (http://juliangut.com/janitor)
4
 *
5
 * @link https://github.com/juliangut/janitor for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/janitor/blob/master/LICENSE
8
 */
9
10
namespace Janitor\Excluder;
11
12
use Janitor\Excluder as ExcluderInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
15
/**
16
 * Maintenance excluder by request header value
17
 */
18
class Header implements ExcluderInterface
19
{
20
    /**
21
     * Request headers.
22
     *
23
     * @var array
24
     */
25
    protected $headers;
26
27
    /**
28
     * @param array|string|null $headers
29
     * @param string|null       $value
30
     */
31
    public function __construct($headers = null, $value = null)
32
    {
33
        if (!is_array($headers)) {
34
            $headers = [$headers => $value];
35
        }
36
37
        foreach ($headers as $headerName => $headerValue) {
38
            $this->addHeader($headerName, $headerValue);
39
        }
40
    }
41
42
    /**
43
     * Add header.
44
     *
45
     * @param string      $header
46
     * @param string|null $value
47
     *
48
     * @return $this
49
     */
50
    public function addHeader($header, $value = null)
51
    {
52
        if (trim($header) !== '') {
53
            $this->headers[trim($header)] = $value;
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function isExcluded(ServerRequestInterface $request)
63
    {
64
        foreach ($this->headers as $header => $value) {
65
            if ($value === null && $request->hasHeader($header)) {
66
                return true;
67
            } elseif ($value !== null && (trim($value) === $request->getHeaderLine($header)
68
                || @preg_match(trim($value), $request->getHeaderLine($header)))
69
            ) {
70
                return true;
71
            }
72
        }
73
74
        return false;
75
    }
76
}
77