Completed
Push — master ( 2510ed...09ec49 )
by Shawn
03:37
created

AssertStagingMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 24
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 4
1
<?php
2
/**
3
 * This file is part of the RHDC Akamai middleware package.
4
 *
5
 * (c) Shawn Iwinski <[email protected]>
6
 *
7
 * For the full copyright and license information, please view
8
 * the LICENSE file that was distributed with this source code.
9
 */
10
namespace Rhdc\Akamai\Middleware\Response;
11
12
use Psr\Http\Message\ResponseInterface;
13
14
class AssertStagingMiddleware implements MiddlewareInterface
15
{
16
    const HEADER = 'X-Akamai-Staging';
17
18 8
    public function __invoke(ResponseInterface $response)
19
    {
20 8
        if (!$response->hasHeader(static::HEADER)) {
21 1
            throw new AssertStagingMiddlewareException(sprintf(
22 1
                'Response does not contain the "%s" header',
23 1
                static::HEADER
24
            ));
25
        }
26
27 7
        $headerValue = $response->getHeader(static::HEADER);
28 7
        if (empty($headerValue) || (stripos(strtolower($headerValue[0]), 'essl') !== 0)) {
29 4
            throw new AssertStagingMiddlewareException(sprintf(
30 4
                '"%s" header does not start with string "ESSL" (actual value = "%s")',
31 4
                static::HEADER,
32
                // Provide full header value instead of just first value
33 4
                $response->getHeaderLine(static::HEADER)
34
            ));
35
        }
36
37 3
        return $response;
38
    }
39
}
40