Completed
Push — master ( 956196...175692 )
by Gaetano
09:59
created

AbstractMatcher::validateConditions()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Repository;
6
7
class AbstractMatcher
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 $repository;
17
18
    /**
19
     * @param Repository $repository
20
     * @todo inject the services needed, not the whole repository
21
     */
22
    public function __construct(Repository $repository)
23
    {
24
        $this->repository = $repository;
25
    }
26
27
    protected function validateConditions(array $conditions)
28
    {
29
        if (count($conditions) == 0) {
30
            throw new \Exception('Content can not be matched because the matching conditions are empty');
31
        }
32
33
        if (count($conditions) > $this->maxConditions) {
34
            throw new \Exception($this->returns . ' can not be matched because multiple matching conditions are specified. Only a single condition is supported');
35
        }
36
37
        foreach ($conditions as $key => $value) {
38
            if (!in_array($key, $this->allowedConditions)) {
39
                throw new \Exception($this->returns . " can not be matched because matching condition '$key' is not supported. Supported conditions are: " .
40
                    implode(', ', $this->allowedConditions));
41
            }
42
        }
43
    }
44
}
45