Completed
Push — master ( cfe8d7...fcc746 )
by André
19:36 queued 06:48
created

CriteriaConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addHandler() 0 4 1
A convertCriteria() 0 12 3
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Legacy\URL\Query;
8
9
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
10
use eZ\Publish\API\Repository\Values\URL\Query\Criterion;
11
use eZ\Publish\Core\Persistence\Database\SelectQuery;
12
13
class CriteriaConverter
14
{
15
    /**
16
     * Criterion handlers.
17
     *
18
     * @var \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler[]
19
     */
20
    protected $handlers;
21
22
    /**
23
     * Construct from an optional array of Criterion handlers.
24
     *
25
     * @param \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler[] $handlers
26
     */
27
    public function __construct(array $handlers = [])
28
    {
29
        $this->handlers = $handlers;
30
    }
31
32
    /**
33
     * Adds handler.
34
     *
35
     * @param \eZ\Publish\Core\Persistence\Legacy\URL\Query\CriterionHandler $handler
36
     */
37
    public function addHandler(CriterionHandler $handler)
38
    {
39
        $this->handlers[] = $handler;
40
    }
41
42
    /**
43
     * Generic converter of criteria into query fragments.
44
     *
45
     * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException if Criterion is not applicable to its target
46
     *
47
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
48
     * @param \eZ\Publish\API\Repository\Values\URL\Query\Criterion $criterion
49
     * @return \eZ\Publish\Core\Persistence\Database\Expression|string
50
     */
51
    public function convertCriteria(SelectQuery $query, Criterion $criterion)
52
    {
53
        foreach ($this->handlers as $handler) {
54
            if ($handler->accept($criterion)) {
55
                return $handler->handle($this, $query, $criterion);
56
            }
57
        }
58
59
        throw new NotImplementedException(
60
            'No visitor available for: ' . get_class($criterion)
61
        );
62
    }
63
}
64