CriteriaConverter::generateQueryString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway;
4
5
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
6
use Kaliop\EzFindSearchEngineBundle\Core\Base\Exceptions\NotImplementedException;
7
8
class CriteriaConverter extends Converter
9
{
10
    protected $queryBuilderClass = '\ezfeZPSolrQueryBuilder';
11
12
    public function __construct($handlers = array(), $queryBuilderClass = null)
13
    {
14
        parent::__construct($handlers);
15
        if ($queryBuilderClass) {
16
            $this->queryBuilderClass = $queryBuilderClass;
17
        }
18
    }
19
20
    /**
21
     * @param Criterion $criterion
22
     * @return bool
23
     */
24
    public function canHandle(Criterion $criterion)
25
    {
26
        /** @var CriterionHandler $handler */
27
        foreach ($this->handlers as $handler) {
28
            if ($handler->accept($criterion)) {
29
                return true;
30
            }
31
        }
32
33
        return false;
34
    }
35
36
    /**
37
     * @param Criterion $criterion
38
     * @return mixed
39
     * @throws NotImplementedException
40
     */
41 View Code Duplication
    public function handle(Criterion $criterion)
0 ignored issues
show
Duplication introduced by
This method 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...
42
    {
43
        /** @var CriterionHandler $handler */
44
        foreach ($this->handlers as $handler) {
45
            if ($handler->accept($criterion)) {
46
                return $handler->handle($this, $criterion);
47
            }
48
        }
49
50
        throw new NotImplementedException(
51
            'No handler available for: ' . get_class($criterion) . ' with operator ' . $criterion->operator
52
        );
53
    }
54
55
    /**
56
     * Given a Solr filter in array form (as passed to ezfeZPSolrQueryBuilder), return its string representtaion
57
     * @param array $filter
58
     * @return string
59
     * @throws NotImplementedException
60
     */
61
    public function generateQueryString($filter)
62
    {
63
        // build the Solr query string via ezfind - we have to resort to
64
        // a hackish way to access a protected method in ezfeZPSolrQueryBuilder
65
        $queryBuilderClass = $this->queryBuilderClass;
66
        /** @var \ezfeZPSolrQueryBuilder $queryBuilder */
67
        $queryBuilder = new $queryBuilderClass(null);
68
        $filterCreator = \Closure::bind(function($filter){return $this->getParamFilterQuery(array('Filter' => $filter));}, $queryBuilder, $queryBuilder);
0 ignored issues
show
Bug introduced by
The method getParamFilterQuery() does not seem to exist on object<Kaliop\EzFindSear...eway\CriteriaConverter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        return $filterCreator($filter);
70
    }
71
72
}
73