Completed
Branch 1707_add_explain_command (e87963)
by Alessandro
08:07
created

ExplainCommandBuilder::getDistinctArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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