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

Query::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 0
crap 4
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\MongoDB\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_COUNT           = 2;
15
    const TYPE_AGGREGATE       = 3;
16
17
    /** @var Collection */
18
    private $collection;
19
    /** @var array  */
20
    private $querySettings;
21
    /** @var array  */
22
    private $options;
23
    /** @var int */
24
    private $type;
25
26
    /**
27
     * Query constructor.
28
     *
29
     * @param Collection $collection
30
     * @param int        $type
31
     * @param array      $querySettings
32
     * @param array      $options
33
     */
34 20
    public function __construct(
35
        Collection $collection,
36
        int $type=self::TYPE_FIND,
37
        array $querySettings=[],
38
        array $options=[]
39
    ) {
40 20
        $this->collection = $collection;
41 20
        $this->querySettings = $querySettings;
42 20
        $this->options = $options;
43 20
        $this->type = $type;
44 20
    }
45
46
    /**
47
     * @return mixed|Cursor
48
     *
49
     * @throws \Exception
50
     */
51 9
    public function execute()
52
    {
53 9
        switch($this->type) {
54 9
            case self::TYPE_FIND:
55 6
                    return $this->collection->find($this->querySettings, $this->options);
56
                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...
57 3
            case self::TYPE_COUNT:
58 1
                    return $this->collection->count($this->querySettings, $this->options);
59
                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...
60 2
            case self::TYPE_AGGREGATE:
61 1
                    return $this->collection->aggregate($this->querySettings, $this->options);
62
                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...
63
            default:
64 1
                throw new \Exception('Unsupported query type ... I\'m sorry');
65
        }
66
    }
67
68
    /**
69
     * @return array
70
     */
71 9
    public function getQuerySettings(): array
72
    {
73 9
        return $this->querySettings;
74
    }
75
76
    /**
77
     * @return array
78
     */
79 8
    public function getOptions(): array
80
    {
81 8
        return $this->options;
82
    }
83
}
84