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

Query::getFilters()   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
use MongoDB\Driver\Cursor;
7
8
/**
9
 * Class Query
10
 */
11
class Query
12
{
13
    const TYPE_FIND            = 1;
14
    const TYPE_FIND_AND_UPDATE = 2;
15
    const TYPE_FIND_AND_REMOVE = 3;
16
    const TYPE_INSERT          = 4;
17
    const TYPE_UPDATE          = 5;
18
    const TYPE_REMOVE          = 6;
19
    const TYPE_GROUP           = 7;
20
    const TYPE_MAP_REDUCE      = 8;
21
    const TYPE_DISTINCT        = 9;
22
    const TYPE_GEO_NEAR        = 10;
23
    const TYPE_COUNT           = 11;
24
25
    /** @var Collection */
26
    private $collection;
27
    /** @var array  */
28
    private $filters;
29
    /** @var array  */
30
    private $options;
31
    /** @var int */
32
    private $type;
33
34
    /**
35
     * Query constructor.
36
     *
37
     * @param Collection $collection
38
     * @param int        $type
39
     * @param array      $filters
40
     * @param array      $options
41
     */
42
    public function __construct(Collection $collection, int $type=self::TYPE_FIND, array $filters=[], array $options=[])
43
    {
44
        $this->collection = $collection;
45
        $this->filters = $filters;
46
        $this->options = $options;
47
        $this->type = $type;
48
    }
49
50
    /**
51
     * @return mixed
52
     *
53
     * @throws \Exception
54
     */
55
    protected function execute()
56
    {
57
        switch($this->type) {
58
            case self::TYPE_FIND:
59
                    return $this->collection->find($this->filters, $this->options);
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61
            case self::TYPE_COUNT:
62
                    return $this->collection->count($this->filters, $this->options);
63
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64
            default:
65
                throw new \Exception('Unsupported query type ... I\'m sorry');
66
        }
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getFilters(): array
73
    {
74
        return $this->filters;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function getOptions(): array
81
    {
82
        return $this->options;
83
    }
84
}
85