Completed
Push — master ( abe227...316baf )
by Raffael
13:55 queued 08:01
created

QueryTransformer::transform()   D

Complexity

Conditions 21
Paths 28

Size

Total Lines 89

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 21.2005

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 48
cts 52
cp 0.9231
rs 4.1666
c 0
b 0
f 0
cc 21
nc 28
nop 1
crap 21.2005

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Endpoint\Ldap;
13
14
class QueryTransformer
15
{
16
    /**
17
     * Convert mongodb like query to ldap query.
18
     */
19 7
    public static function transform($query): string
20
    {
21 7
        $result = '';
22
23 7
        if (!is_array($query)) {
24
            return $query;
25
        }
26
27 7
        $simple = '';
28 7
        foreach ($query as $key => $value) {
29 7
            if (is_array($value) && isset($value['$and']) || isset($value['$or'])) {
30
                $result .= self::transform($value);
31
32
                continue;
33
            }
34
35 7
            $key = (string) $key;
36 7
            switch ($key) {
37 7
                case '$and':
38 2
                    $part = '(&';
39 2
                    foreach ($value as $sub) {
40 2
                        $part .= self::transform($sub);
41
                    }
42
43 2
                    $result .= $part.')';
44
45 2
                break;
46 7
                case '$or':
47 3
                    $part = '(|';
48 3
                    foreach ($value as $sub) {
49 3
                        $part .= self::transform($sub);
50
                    }
51
52 3
                    $result .= $part.')';
53
54 3
                break;
55
                default:
56 7
                    $part = '';
57
58 7
                    if (is_array($value)) {
59 2
                        foreach ($value as $t => $a) {
60 2
                            if (!is_array($a) && $t[0] !== '$') {
61
                                $part .= '('.$t.'='.$a.')';
62
                            }
63
64 2
                            switch ($t) {
65 2
                                case '$gt':
66 2
                                    $part .= '('.$key.'>'.$a.')';
67
68 2
                                break;
69 2
                                case '$lt':
70 2
                                    $part .= '('.$key.'<'.$a.')';
71
72 2
                                break;
73 1
                                case '$lte':
74 1
                                    $part .= '('.$key.'<='.$a.')';
75
76 1
                                break;
77 1
                                case '$gte':
78 1
                                    $part .= '('.$key.'>='.$a.')';
79
80 2
                                break;
81
                            }
82
                        }
83
84 2
                        if (count($value) > 1) {
85 1
                            $result .= '(&'.$part.')';
86
                        } else {
87 2
                            $result .= $part;
88
                        }
89
                    } else {
90 5
                        $simple .= '('.$key.'='.$value.')';
91
                    }
92
93 7
                break;
94
            }
95
        }
96
97 7
        if (!empty($simple)) {
98 5
            if (count($query) > 1) {
99 2
                $simple = '(&'.$simple.')';
100 2
                $result .= $simple;
101
            } else {
102 3
                $result .= $simple;
103
            }
104
        }
105
106 7
        return $result;
107
    }
108
109
    protected static function transformOperator($value)
110
    {
111
        return $value;
112
    }
113
}
114