HeaderIsExpectation::from()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 10
cc 4
nc 1
nop 1
crap 4
1
<?php
2
3
namespace EasyHttp\MockBuilder\Expectations;
4
5
use EasyHttp\MockBuilder\Helpers\GuzzleHeaderParser;
6
use EasyHttp\MockBuilder\Contracts\ExpectationMatcher;
7
use EasyHttp\MockBuilder\Expectation;
8
use GuzzleHttp\Promise\RejectedPromise;
9
use Psr\Http\Message\RequestInterface;
10
11
class HeaderIsExpectation implements ExpectationMatcher
12
{
13 121
    public static function from(Expectation $expectation): callable
14
    {
15 121
        return function ($request) use ($expectation) {
16
            /** @var RequestInterface $request */
17 87
            $headers = GuzzleHeaderParser::parse($request->getHeaders());
18
19 87
            foreach ($expectation->notEmptyHeadersIterator() as $header => $value) {
20 14
                if (!array_key_exists($header, $headers)) {
21 3
                    return new RejectedPromise('header \'' . $header . '\' is missing');
22
                }
23
24 11
                if ($headers[$header] !== $value) {
25 7
                    return new RejectedPromise('header \'' . $header . '\' does not match expectation');
26
                }
27
            }
28
29 77
            return $request;
30 121
        };
31
    }
32
}
33