Passed
Pull Request — master (#10)
by Gocha
11:52
created

QueryAnalyser::analyseQuery()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 0
cts 18
cp 0
rs 9.584
c 0
b 0
f 0
cc 4
nc 2
nop 2
crap 20
1
<?php
2
declare(strict_types=1);
3
4
namespace Paysera\Pagination\Service\ODM;
5
6
use Paysera\Pagination\Entity\ODM\AnalysedQuery;
7
use Paysera\Pagination\Entity\OrderingConfiguration;
8
use Paysera\Pagination\Entity\ODM\ConfiguredQuery;
9
use Paysera\Pagination\Entity\OrderingPair;
10
use Paysera\Pagination\Entity\Pager;
11
use Paysera\Pagination\Exception\TooLargeOffsetException;
12
13
class QueryAnalyser
14
{
15
    const ID_FIELD = 'id';
16
17
    /**
18
     * @internal
19
     * @param ConfiguredQuery $configuredQuery
20
     * @param Pager $pager
21
     * @return AnalysedQuery
22
     */
23 View Code Duplication
    public function analyseQuery(ConfiguredQuery $configuredQuery, Pager $pager)
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...
24
    {
25
        $analysedQuery = $this->analyseQueryWithoutPager($configuredQuery);
26
27
        if (
28
            $pager->getOffset() !== null
29
            && $configuredQuery->hasMaximumOffset()
30
            && $pager->getOffset() > $configuredQuery->getMaximumOffset()
31
        ) {
32
            throw new TooLargeOffsetException($configuredQuery->getMaximumOffset(), $pager->getOffset());
33
        }
34
35
        $orderingConfigurations = $this->mapToOrderingConfigurations(
36
            $configuredQuery,
37
            $pager->getOrderingPairs()
38
        );
39
40
        return $analysedQuery
41
            ->setOrderingConfigurations($orderingConfigurations)
42
        ;
43
    }
44
45
    /**
46
     * @internal
47
     * @param ConfiguredQuery $configuredQuery
48
     * @return AnalysedQuery
49
     */
50
    public function analyseQueryWithoutPager(ConfiguredQuery $configuredQuery)
51
    {
52
        $queryBuilder = $configuredQuery->getQueryBuilder();
53
54
        return (new AnalysedQuery())
55
            ->setQueryBuilder($queryBuilder)
56
        ;
57
    }
58
59
    /**
60
     * @param ConfiguredQuery $configuredQuery
61
     * @param array|OrderingPair[] $orderingPairs
62
     * @return array|OrderingConfiguration[]
63
     */
64 View Code Duplication
    private function mapToOrderingConfigurations(ConfiguredQuery $configuredQuery, array $orderingPairs)
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...
65
    {
66
        $orderingConfigurations = [];
67
        $idIncluded = false;
68
        $defaultAscending = null;
69
        $orderByIdExpression = self::ID_FIELD;
70
71
        foreach ($orderingPairs as $orderingPair) {
72
            $orderingConfiguration = $configuredQuery->getOrderingConfigurationFor($orderingPair->getOrderBy());
73
74
            if ($orderingPair->isOrderingDirectionSet()) {
75
                $orderingConfiguration->setOrderAscending($orderingPair->isOrderAscending());
76
            }
77
78
            if ($orderingConfiguration->getOrderByExpression() === $orderByIdExpression) {
79
                $idIncluded = true;
80
            }
81
82
            if ($defaultAscending === null) {
83
                $defaultAscending = $orderingConfiguration->isOrderAscending();
84
            }
85
86
            $orderingConfigurations[] = $orderingConfiguration;
87
        }
88
89
        if ($idIncluded) {
90
            return $orderingConfigurations;
91
        }
92
93
        $orderingConfigurations[] = (new OrderingConfiguration($orderByIdExpression, self::ID_FIELD))
94
            ->setOrderAscending($defaultAscending ?? false)
95
        ;
96
97
        return $orderingConfigurations;
98
    }
99
}
100