Completed
Push — master ( db1c89...2f2a87 )
by Alessandro
01:36
created

Query::getQuery()   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 Query
9
 */
10
class Query
11
{
12
    const TYPE_FIND            = 1;
13
    const TYPE_FIND_AND_UPDATE = 2;
14
    const TYPE_FIND_AND_REMOVE = 3;
15
    const TYPE_INSERT          = 4;
16
    const TYPE_UPDATE          = 5;
17
    const TYPE_REMOVE          = 6;
18
    const TYPE_GROUP           = 7;
19
    const TYPE_MAP_REDUCE      = 8;
20
    const TYPE_DISTINCT        = 9;
21
    const TYPE_GEO_NEAR        = 10;
22
    const TYPE_COUNT           = 11;
23
24
    /** @var Collection */
25
    private $collection;
26
    /** @var array  */
27
    private $query;
28
    /** @var array  */
29
    private $options;
30
    /** @var int */
31
    private $type;
32
33
    /**
34
     * Query constructor.
35
     *
36
     * @param Collection $collection
37
     * @param array      $query
38
     * @param array      $options
39
     */
40
    public function __construct(Collection $collection, array $query = [], array $options = [])
41
    {
42
        $this->collection = $collection;
43
        $this->query = $query;
44
        $this->options = $options;
45
        $this->type = $query['type'] ?? self::TYPE_FIND;
46
    }
47
48
    /**
49
     * @return mixed
50
     *
51
     * @throws \Exception
52
     */
53
    public function execute()
54
    {
55
        switch($this->type) {
56
            case self::TYPE_FIND:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
57
                    return $this->collection->find($this->query, $this->options);
58
                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...
59
            default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
60
                throw new \Exception('Unsupported query type ... I\'m sorry');
61
        }
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getQuery(): array
68
    {
69
        return $this->query;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getOptions(): array
76
    {
77
        return $this->options;
78
    }
79
}
80