Completed
Push — master ( 310596...ecf895 )
by Hannes
02:21
created

FirstOf   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 27
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A match() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\autogiro\Parser\Matcher;
6
7
use byrokrat\autogiro\Line;
8
use byrokrat\autogiro\Exception\InvalidContentException;
9
10
/**
11
 * Grab content of the first valid sub-matcher
12
 */
13
class FirstOf implements Matcher
14
{
15
    /**
16
     * @var Matcher[] Loaded matchers
17
     */
18
    private $matchers;
19
20 2
    public function __construct(Matcher ...$matchers)
21
    {
22 2
        $this->matchers = $matchers;
23 2
    }
24
25 2
    public function match(Line $line): string
26
    {
27 2
        foreach ($this->matchers as $matcher) {
28
            try {
29 1
                return $matcher->match($line);
30 1
            } catch (InvalidContentException $exception) {
31
                // Skipping invalid matches in favour of first valid match
32
            }
33
        }
34
35 1
        throw new InvalidContentException(
36 1
            'Unable to find a match using a set of matchers'
37
        );
38
    }
39
}
40