Completed
Push — master ( 349bb5...81542e )
by Alessandro
02:39
created

Expression::or()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\MongoDB\QueryBuilder;
4
5
/**
6
 * Class Expression.
7
 */
8
class Expression
9
{
10
    /** @var array */
11
    private $filters;
12
13
    /**
14
     * Expression constructor.
15
     */
16 20
    public function __construct()
17
    {
18 20
        $this->filters = [];
19 20
    }
20
21
    /**
22
     * @param array|Expression $expression
23
     *
24
     * @return $this
25
     */
26 View Code Duplication
    public function and ($expression)
0 ignored issues
show
Unused Code introduced by
The parameter $expression 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...
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...
27
    {
28 9
        $this->prepareFilterIndex('$and');
29
30 9
        $this->filters['$and'] = array_merge(
31 9
            $this->filters['$and'],
32 9
            $this->mapExpressions(...func_get_args())
33
        );
34
35 9
        return $this;
36
    }
37
38
    /**
39
     * @param string $operator
40
     */
41 15
    private function prepareFilterIndex(string $operator)
42
    {
43 15
        if (!isset($this->filters[$operator])) {
44 15
            $this->filters[$operator] = [];
45
        }
46 15
    }
47
48
    /**
49
     * @param  $expressions
50
     *
51
     * @return array
52
     */
53 10
    private function mapExpressions($expressions): array
0 ignored issues
show
Unused Code introduced by
The parameter $expressions 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...
54
    {
55 10
        return array_map(
56
            function ($expression) {
57 10
                return $expression instanceof Expression ? $expression->getExpressionFilters() : $expression;
58 10
            },
59
            func_get_args()
60
        );
61
    }
62
63
    /**
64
     * @return array
65
     */
66 20
    public function getExpressionFilters(): array
67
    {
68 20
        return $this->filters;
69
    }
70
71
    /**
72
     * @param array|Expression $expression
73
     *
74
     * @return $this
75
     */
76 3 View Code Duplication
    public function or ($expression)
0 ignored issues
show
Unused Code introduced by
The parameter $expression 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...
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...
77
    {
78 3
        $this->prepareFilterIndex('$or');
79
80 3
        $this->filters['$or'] = array_merge(
81 3
            $this->filters['$or'],
82 3
            $this->mapExpressions(...func_get_args())
83
        );
84
85 3
        return $this;
86
    }
87
88
    /**
89
     * @param string $field
90
     * @param string $value
91
     *
92
     * @return $this
93
     */
94 2
    public function equal(string $field, $value)
95
    {
96 2
        $this->prepareFilterIndex($field);
97
98 2
        $this->filters[$field] = $this->operationExpression('$eq', $value);
99
100 2
        return $this;
101
    }
102
103
    /**
104
     * @param string $operation
105
     * @param mixed  $value
106
     *
107
     * @return array
108
     */
109 6
    private function operationExpression(string $operation, $value): array
110
    {
111 6
        return [$operation => $value];
112
    }
113
114
    /**
115
     * @param string $field
116
     * @param string $value
117
     *
118
     * @return $this
119
     */
120 4
    public function notEqual(string $field, $value)
121
    {
122 4
        $this->prepareFilterIndex($field);
123
124 4
        $this->filters[$field] = $this->operationExpression('$ne', $value);
125
126 4
        return $this;
127
    }
128
}
129