Completed
Push — master ( 5a434d...bba42e )
by Alessandro
01:37
created

Builder::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\QueryBuilder;
4
5
use MongoDB\Collection;
6
7
/**
8
 * Class Builder.
9
 */
10
class Builder
11
{
12
    /** @var Collection */
13
    private $collection;
14
    /** @var Expression  */
15
    private $expression;
16
    /** @var array */
17
    private $options;
18
19
    /**
20
     * Builder constructor.
21
     *
22
     * @param Collection $collection
23
     */
24
    public function __construct(Collection $collection)
25
    {
26
        $this->collection = $collection;
27
        $this->queryType = Query::TYPE_FIND;
0 ignored issues
show
Bug introduced by
The property queryType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        $this->expression = new Expression();
29
        $this->options = [];
30
    }
31
32
    /**
33
     * @return $this
34
     */
35
    public function find()
36
    {
37
        return $this->setType(Query::TYPE_FIND);
38
    }
39
40
    /**
41
     * @return $this
42
     */
43
    public function count()
44
    {
45
        return $this->setType(Query::TYPE_COUNT);
46
    }
47
48
    /**
49
     * @param int $type
50
     *
51
     * @return $this
52
     */
53
    protected function setType(int $type)
54
    {
55
        $this->queryType = $type;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param array|Expression $expression
62
     *
63
     * @return $this
64
     */
65
    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...
66
    {
67
        $this->expression->and(...func_get_args());
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param array|Expression $expression
74
     *
75
     * @return $this
76
     */
77
    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...
78
    {
79
        $this->expression->or(...func_get_args());
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return Query
86
     */
87
    public function getQuery(): Query
88
    {
89
        return new Query(
90
            $this->collection,
91
            $this->queryType,
92
            $this->expression->getExpressionFilters(),
93
            $this->options
94
        );
95
    }
96
97
    /**
98
     * @param array $fields
99
     */
100
    public function sort(array $fields)
101
    {
102
        $this->options['sort'] = $fields;
103
    }
104
105
    /**
106
     * @param int $limit
107
     */
108
    public function setMaxResults(int $limit)
109
    {
110
        $this->options['limit'] = $limit;
111
    }
112
113
    /**
114
     * @param array $projection
115
     */
116
    public function select(array $projection)
117
    {
118
        $this->options['projection'] = $projection;
119
    }
120
121
    /**
122
     * @return Expression
123
     */
124
    public function expr(): Expression
125
    {
126
        return new Expression();
127
    }
128
}