Completed
Push — EZP-31084-part2-poc ( f20bda )
by André
19:16
created

CriteriaConverter::addHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 4
loc 4
rs 10
c 0
b 0
f 0
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\Content\Query;
8
9
use Doctrine\DBAL\Query\QueryBuilder;
10
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
11
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
12
13 View Code Duplication
class CriteriaConverter
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * Criterion handlers.
17
     *
18
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\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\Content\Query\CriterionHandler[] $handlers
26
     */
27
    public function __construct(iterable $handlers = [])
28
    {
29
        $this->handlers = $handlers;
30
    }
31
32
    /**
33
     * Adds handler.
34
     *
35
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\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 \Doctrine\DBAL\Query\QueryBuilder $query
48
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
49
     *
50
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression|string Expression to be used in where clause of query
51
     */
52
    public function convertCriteria(QueryBuilder $query, Criterion $criterion)
53
    {
54
        foreach ($this->handlers as $handler) {
55
            if ($handler->accept($criterion)) {
56
                return $handler->handle($this, $query, $criterion);
57
            }
58
        }
59
60
        throw new NotImplementedException(
61
            'No visitor available for: ' . get_class($criterion)
62
        );
63
    }
64
}
65