Completed
Push — master ( fa3295...4abae7 )
by Gaetano
06:21
created

AbstractMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use Kaliop\eZMigrationBundle\API\MatcherInterface;
6
7
abstract class AbstractMatcher implements MatcherInterface
8
{
9
    /** @var string[] $allowedConditions the keywords we allow to be used for matching on*/
10
    protected $allowedConditions = array();
11
    /** @var  string $returns user-readable name of the type of object returned */
12
    protected $returns;
13
    /** @var int $maxConditions the maximum number of conditions we allow to match on for a single match request */
14
    protected $maxConditions = 1;
15
16
    protected function validateConditions(array $conditions)
17
    {
18
        if (count($conditions) == 0) {
19
            throw new \Exception($this->returns . ' can not be matched because the matching conditions are empty');
20
        }
21
22
        if (count($conditions) > $this->maxConditions) {
23 20
            throw new \Exception($this->returns . " can not be matched because multiple matching conditions are specified. Only {this->maxConditions} condition(s) are supported");
24
        }
25 20
26 20
        foreach ($conditions as $key => $value) {
27
            if (!in_array((string)$key, $this->allowedConditions)) {
28 3
                throw new \Exception($this->returns . " can not be matched because matching condition '$key' is not supported. Supported conditions are: " .
29
                    implode(', ', $this->allowedConditions));
30 3
            }
31
        }
32
    }
33
34 3
    public function matchOne(array $conditions)
35
    {
36
        $results = $this->match($conditions);
37
        $count = count($results);
38 3
        if ($count !== 1) {
39 3
            throw new \Exception("Found $count " . $this->returns . " when expected exactly only one to match the conditions");
40
        }
41
        return reset($results);
42
    }
43 3
44 3
    abstract public function match(array $conditions);
45
}
46