Completed
Push — master ( f1592e...b3630f )
by Daniel
03:25 queued 01:30
created

Query::create()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 6
Ratio 25 %

Importance

Changes 0
Metric Value
dl 6
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ObjectAgent\Query;
6
7
final class Query
8
{
9
    private $classFqn;
10
    private $expression;
11
    private $orderings;
12
    private $firstResult;
13
    private $maxResults;
14
    private $joins;
15
    private $selects;
16
17
    private function __construct(
18
        string $classFqn,
19
        array $selects = [],
20
        array $joins = [],
21
        Expression $expression = null,
22
        array $orderings = [],
23
        int $firstResult = null,
24
        int $maxResults = null
25
    ) {
26
        $this->classFqn = $classFqn;
27
        $this->expression = $expression;
28
        $this->orderings = $orderings;
29
        $this->firstResult = $firstResult;
30
        $this->maxResults = $maxResults;
31
        $this->joins = $joins;
32
        $this->selects = $selects;
33
34
        array_walk($joins, function (Join $join) {
0 ignored issues
show
Unused Code introduced by
The parameter $join is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
        });
36
    }
37
38
    public static function create(
39
        string $classFqn,
40
        array $query = []
41
    ) {
42
        $defaults = [
43
            'selects' => [],
44
            'criteria' => null,
45
            'orderings' => [],
46
            'joins' => [],
47
            'firstResult' => null,
48
            'maxResults' => null,
49
        ];
50
51 View Code Duplication
        if ($diff = array_diff(array_keys($query), array_keys($defaults))) {
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...
52
            throw new \InvalidArgumentException(sprintf(
53
                'Invalid query keys "%s", valid keys: "%s"',
54
                implode('", "', $diff), implode('", "', array_keys($defaults))
55
            ));
56
        }
57
58
        $query = array_merge($defaults, $query);
59
60
        return new self($classFqn, $query['selects'], $query['joins'], $query['criteria'], $query['orderings'], $query['firstResult'], $query['maxResults']);
61
    }
62
63
    public static function comparison(string $comparator, $value1, $value2): Comparison
64
    {
65
        return new Comparison($comparator, $value1, $value2);
66
    }
67
68
    public static function composite(string $type, ...$expressions): Composite
69
    {
70
        return new Composite($type, $expressions);
71
    }
72
73
    public static function join(string $type, string $alias)
74
    {
75
        return new Join($type, $alias);
76
    }
77
78
    public function cloneWith(array $parts)
79
    {
80
        return self::create(
81
            $this->getClassFqn(),
82
            [
83
                'selects' => array_key_exists('selects', $parts) ? $parts['selects'] : $this->selects,
84
                'criteria' => array_key_exists('criteria', $parts) ? $parts['criteria'] : $this->expression,
85
                'orderings' => array_key_exists('orderings', $parts) ? $parts['orderings'] : $this->orderings,
86
                'joins' => array_key_exists('joins', $parts) ? $parts['joins'] : $this->joins,
87
                'firstResult' => array_key_exists('firstResult', $parts) ? $parts['firstResult'] : $this->firstResult,
88
                'maxResults' => array_key_exists('maxResults', $parts) ? $parts['maxResults'] : $this->maxResults,
89
            ]
90
        );
91
    }
92
93
    public function getClassFqn(): string
94
    {
95
        return $this->classFqn;
96
    }
97
98
    public function hasExpression()
99
    {
100
        return null !== $this->expression;
101
    }
102
103
    public function getExpression(): Expression
104
    {
105
        return $this->expression;
106
    }
107
108
    public function getOrderings(): array
109
    {
110
        return $this->orderings;
111
    }
112
113
    public function getMaxResults()
114
    {
115
        return $this->maxResults;
116
    }
117
118
    public function getFirstResult()
119
    {
120
        return $this->firstResult;
121
    }
122
123
    public function getJoins(): array
124
    {
125
        return $this->joins;
126
    }
127
128
    public function getSelects(): array
129
    {
130
        return $this->selects;
131
    }
132
}
133