ExplainCommandBuilder::manageDistinct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Services\Explain;
6
7
use Facile\MongoDbBundle\Models\Query;
8
9
class ExplainCommandBuilder
10
{
11
    /**
12
     * @param Query $query
13
     * @param string $verbosity
14
     *
15
     * @throws \Exception
16 11
     *
17
     * @return array
18
     */
19
    public static function createCommandArgs(
20 11
        Query $query,
21
        string $verbosity = ExplainQueryService::VERBOSITY_ALL_PLAN_EXECUTION
22 1
    ): array {
23 1
        if ('aggregate' === $query->getMethod()) {
24
            return [
25
                'aggregate' => $query->getCollection(),
26
                'pipeline' => $query->getData(),
27
                'explain' => true,
28
            ];
29 10
        }
30
31
        $args = [
32 10
            $query->getMethod() => $query->getCollection(),
33 10
        ];
34 10
35 10
        $args = self::manageCount($query, $args);
36
        $args = self::manageDistinct($query, $args);
37
        $args = self::manageFind($query, $args);
38 10
        $args = self::manageDelete($query, $args);
39 10
40
        return [
41
            'explain' => $args,
42
            'verbosity' => $verbosity,
43
        ];
44
    }
45
46
    /**
47
     * @param Query $query
48
     * @param       $args
49 10
     *
50
     * @return array
51 10
     */
52
    private static function manageCount(Query $query, array $args): array
53 2
    {
54
        if ('count' === $query->getMethod()) {
55
            $args += [
56 2
                'query' => $query->getFilters(),
57 2
            ];
58
59
            foreach (['limit', 'hint', 'skip'] as $supportedOption) {
60
                $args += (isset($query->getOptions()[$supportedOption]) ? [$supportedOption => $query->getOptions()[$supportedOption]] : []);
61 10
            }
62
        }
63
64
        return $args;
65
    }
66
67
    /**
68
     * @param Query $query
69
     * @param       $args
70 10
     *
71
     * @return array
72 10
     */
73
    private static function manageDistinct(Query $query, array $args): array
74 1
    {
75 1
        if ('distinct' === $query->getMethod()) {
76
            $args += [
77
                'key' => $query->getData()['fieldName'],
78
                'query' => $query->getFilters(),
79 10
            ];
80
        }
81
82
        return $args;
83
    }
84
85
    /**
86
     * @param Query $query
87
     * @param array $args
88 10
     *
89
     * @return array
90 10
     */
91
    private static function manageFind(Query $query, array $args): array
92 5
    {
93 5
        if (\in_array($query->getMethod(), ['find', 'findOne', 'findOneAndUpdate', 'findOneAndDelete'])) {
94
            $args = [
95
                'find' => $query->getCollection(),
96 5
                'filter' => $query->getFilters(),
97 5
            ];
98
99
            foreach (['sort', 'projection', 'hint', 'skip', 'limit'] as $supportedOption) {
100
                $args += (isset($query->getOptions()[$supportedOption]) ? [$supportedOption => $query->getOptions()[$supportedOption]] : []);
101 10
            }
102
        }
103
104
        return $args;
105
    }
106
107
    /**
108
     * @param Query $query
109
     * @param array $args
110 10
     *
111
     * @return array
112 10
     */
113
    private static function manageDelete(Query $query, array $args): array
114 2
    {
115
        if (\in_array($query->getMethod(), ['deleteOne', 'deleteMany'])) {
116 2
            return [
117
                'delete' => $query->getCollection(),
118
                'deletes' => [
119
                    ['q' => $query->getFilters(), 'limit' => $query->getOptions()['limit'] ?? 0],
120
                ],
121 8
            ];
122
        }
123
124
        return $args;
125
    }
126
}
127