Completed
Branch 1707_add_explain_command (cbe7fb)
by Alessandro
02:54
created

ExplainCommandBuilder::createCommandArgs()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Facile\MongoDbBundle\Services\Explain;
4
5
use Facile\MongoDbBundle\Models\Query;
6
7
class ExplainCommandBuilder
8
{
9
    /**
10
     * @param Query  $query
11
     * @param string $verbosity
12
     *
13
     * @return array
14
     * @throws \Exception
15
     */
16 11
    public static function createCommandArgs(
17
        Query $query,
18
        string $verbosity = ExplainQueryService::VERBOSITY_ALL_PLAN_EXECUTION
19
    ): array
20
    {
21 11
        if ('aggregate' === $query->getMethod()) {
22
            return [
23 1
                'aggregate' => $query->getCollection(),
24 1
                'pipeline' => $query->getData(),
25
                'explain' => true,
26
            ];
27
        }
28
29
        $args = [
30 10
            $query->getMethod() => $query->getCollection(),
31
        ];
32
33 10
        $args = self::manageCount($query, $args);
34 10
        $args = self::manageDistinct($query, $args);
35 10
        $args = self::manageFind($query, $args);
36 10
        $args = self::manageDelete($query, $args);
37
38
        return [
39 10
            'explain' => $args,
40 10
            'verbosity' => $verbosity,
41
        ];
42
    }
43
44
    /**
45
     * @param Query $query
46
     * @param       $args
47
     *
48
     * @return array
49
     */
50 10
    private static function manageCount(Query $query, array $args): array
51
    {
52 10
        if ('count' === $query->getMethod()) {
53
            $args += [
54 2
                'query' => $query->getFilters(),
55
            ];
56
57 2 View Code Duplication
            foreach (['limit', 'hint', 'skip'] as $supportedOption) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 2
                $args += (isset($query->getOptions()[$supportedOption]) ? [$supportedOption => $query->getOptions()[$supportedOption]] : []);
59
            }
60
        }
61
62 10
        return $args;
63
    }
64
65
    /**
66
     * @param Query $query
67
     * @param       $args
68
     *
69
     * @return array
70
     */
71 10
    private static function manageDistinct(Query $query, array $args): array
72
    {
73 10
        if ('distinct' === $query->getMethod()) {
74
            $args += [
75 1
                'key' => $query->getData()['fieldName'],
76 1
                'query' => $query->getFilters(),
77
            ];
78
        }
79
80 10
        return $args;
81
    }
82
83
    /**
84
     * @param Query $query
85
     * @param array $args
86
     *
87
     * @return array
88
     */
89 10
    private static function manageFind(Query $query, array $args): array
90
    {
91 10
        if (in_array($query->getMethod(), ['find', 'findOne', 'findOneAndUpdate', 'findOneAndDelete'])) {
92
            $args = [
93 5
                'find' => $query->getCollection(),
94 5
                'filter' => $query->getFilters(),
95
            ];
96
97 5 View Code Duplication
            foreach (['sort', 'projection', 'hint', 'skip', 'limit'] as $supportedOption) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98 5
                $args += (isset($query->getOptions()[$supportedOption]) ? [$supportedOption => $query->getOptions()[$supportedOption]] : []);
99
            }
100
        }
101
102 10
        return $args;
103
    }
104
105
    /**
106
     * @param Query $query
107
     * @param array $args
108
     *
109
     * @return array
110
     */
111 10
    private static function manageDelete(Query $query, array $args): array
112
    {
113 10
        if (in_array($query->getMethod(), ['deleteOne', 'deleteMany'])) {
114
            return [
115 2
                'delete' => $query->getCollection(),
116
                'deletes' => [
117 2
                    ['q' => $query->getFilters(), 'limit' => $query->getOptions()['limit'] ?? 0,]
118
                ]
119
            ];
120
        }
121
122 8
        return $args;
123
    }
124
}