Completed
Push — develop ( 99c8cd...370699 )
by Baptiste
02:34
created

DelegationExtractor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B extract() 0 25 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\RangeExtractor;
5
6
use Innmind\Rest\ServerBundle\Exception\{
7
    RangeNotFoundException,
8
    InvalidArgumentException
9
};
10
use Innmind\Rest\Server\Request\Range;
11
use Innmind\Http\Message\ServerRequestInterface;
12
use Innmind\Immutable\SetInterface;
13
14
final class DelegationExtractor implements ExtractorInterface
15
{
16
    private $extractors;
17
18
    public function __construct(SetInterface $extractors)
19
    {
20
        if ((string) $extractors->type() !== ExtractorInterface::class) {
21
            throw new InvalidArgumentException;
22
        }
23
24
        $this->extractors = $extractors;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function extract(ServerRequestInterface $request): Range
31
    {
32
        $range = $this
33
            ->extractors
34
            ->reduce(
35
                null,
36
                function($carry, ExtractorInterface $extractor) use ($request) {
37
                    if ($carry instanceof Range) {
38
                        return $carry;
39
                    }
40
41
                    try {
42
                        return $extractor->extract($request);
43
                    } catch (RangeNotFoundException $e) {
44
                        //pass
45
                    }
46
                }
47
            );
48
49
        if ($range instanceof Range) {
50
            return $range;
51
        }
52
53
        throw new RangeNotFoundException;
54
    }
55
}
56