Completed
Push — EZP-26146-location-swap-urlali... ( 8c8e66...1d10ee )
by
unknown
327:46 queued 295:37
created

Query::processCriteriaArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 9
rs 9.6666
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
 * @version //autogentag//
8
 */
9
namespace eZ\Publish\Core\REST\Server\Input\Parser;
10
11
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
12
use eZ\Publish\Core\REST\Server\Input\Parser\Criterion as CriterionParser;
13
use eZ\Publish\API\Repository\Values\Content\Query\Criterion as CriterionValue;
14
15
/**
16
 * Content/Location Query Parser.
17
 */
18
abstract class Query extends CriterionParser
19
{
20
    /**
21
     * Parses input structure to a Query.
22
     *
23
     * @param array $data
24
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
25
     *
26
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
27
     *
28
     * @return \eZ\Publish\API\Repository\Values\Content\Query
29
     */
30
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
31
    {
32
        $query = $this->buildQuery();
33
34
        // Criteria
35
        // -- FullTextCriterion
36
        if (array_key_exists('Criteria', $data) && is_array($data['Criteria'])) {
37
            $message = 'The Criteria element is deprecated since ezpublish-kernel 6.6.0, and will be removed in 7.0. Use Filter instead.';
38
            if (array_key_exists('Filter', $data) && is_array($data['Filter'])) {
39
                $message .= ' The Criteria element will be merged into Filter.';
40
                $data['Filter'] = array_merge($data['Filter'], $data['Criteria']);
41
            } else {
42
                $data['Filter'] = $data['Criteria'];
43
            }
44
45
            @trigger_error($message, E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
46
        }
47
48 View Code Duplication
        if (array_key_exists('Filter', $data) && is_array($data['Filter'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
            $query->filter = $this->processCriteriaArray($data['Filter'], $parsingDispatcher);
50
        }
51
52 View Code Duplication
        if (array_key_exists('Query', $data) && is_array($data['Query'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $query->query = $this->processCriteriaArray($data['Query'], $parsingDispatcher);
54
        }
55
56
        // limit
57
        if (array_key_exists('limit', $data)) {
58
            $query->limit = (int)$data['limit'];
59
        }
60
61
        // offset
62
        if (array_key_exists('offset', $data)) {
63
            $query->offset = (int)$data['offset'];
64
        }
65
66
        // SortClauses
67
        // -- [SortClauseName: direction|data]
68
        if (array_key_exists('SortClauses', $data)) {
69
            $sortClauses = [];
70
            foreach ($data['SortClauses'] as $sortClauseName => $sortClauseData) {
71
                $sortClauses[] = $this->dispatchSortClause($sortClauseName, $sortClauseData, $parsingDispatcher);
72
            }
73
            $query->sortClauses = $sortClauses;
74
        }
75
76
        // FacetBuilders
77
        // -- contentTypeFacetBuilder
78
        if (array_key_exists('FacetBuilders', $data)) {
79
        }
80
81
        return $query;
82
    }
83
84
    /**
85
     * Builds and returns the Query (Location or Content object).
86
     * @return \eZ\Publish\API\Repository\Values\Content\Query
87
     */
88
    abstract protected function buildQuery();
89
90
    /**
91
     * @param array $criteriaArray
92
     * @param ParsingDispatcher $parsingDispatcher
93
     *
94
     * @return CriterionValue
95
     */
96
    private function processCriteriaArray(array $criteriaArray, ParsingDispatcher $parsingDispatcher)
97
    {
98
        $criteria = array();
99
        foreach ($criteriaArray as $criterionName => $criterionData) {
100
            $criteria[] = $this->dispatchCriterion($criterionName, $criterionData, $parsingDispatcher);
101
        }
102
103
        return (count($criteria) === 1) ? $criteria[0] : new CriterionValue\LogicalAnd($criteria);
104
    }
105
}
106