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

AbstractMatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 39
c 0
b 0
f 0
rs 10
ccs 10
cts 14
cp 0.7143

3 Methods

Rating   Name   Duplication   Size   Complexity  
B validateConditions() 0 17 5
A matchOne() 0 9 2
match() 0 1 ?
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