Passed
Push — master ( c44d00...13b3c2 )
by David
53s
created

HeaderFetcher::getHeaderLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
4
namespace TheCodingMachine\Middlewares\OriginFetchers;
5
6
use Psr\Http\Message\ServerRequestInterface;
7
use TheCodingMachine\Middlewares\CsrfHeaderCheckMiddlewareException;
8
9
trait HeaderFetcher
10
{
11
    /**
12
     * Returns the header, throws an exception if the header is specified more that one in the request.
13
     * Returns null if nothing found.
14
     *
15
     * @param ServerRequestInterface $request
16
     * @param string $header
17
     * @return string|null
18
     * @throws CsrfHeaderCheckMiddlewareException
19
     */
20
    protected function getHeaderLine(ServerRequestInterface $request, string $header)
21
    {
22
        $values = $request->getHeader($header);
23
        if (count($values) > 1) {
24
            throw new CsrfHeaderCheckMiddlewareException("Unexpected request: more than one $header header sent.");
25
        }
26
        if (count($values) === 1) {
27
            return $values[0];
28
        }
29
        return null;
30
    }
31
}
32