QuickInsertFunctions   B
last analyzed

Complexity

Total Complexity 48

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 122
dl 0
loc 217
rs 8.5599
c 0
b 0
f 0
wmc 48

11 Methods

Rating   Name   Duplication   Size   Complexity  
A parsePersistColumns() 0 20 5
A extract() 0 9 1
A parseUpdateResult() 0 16 5
A buildWhere() 0 23 6
A numeric() 0 21 5
A value() 0 16 4
A init() 0 19 5
A slashes() 0 14 5
A getTable() 0 17 5
A buildExtra() 0 22 4
A makeValues() 0 10 3

How to fix   Complexity   

Complex Class

Complex classes like QuickInsertFunctions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use QuickInsertFunctions, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Sludio\HelperBundle\Script\Repository;
4
5
use Sludio\HelperBundle\Script\Utils\Helper;
6
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
7
8
abstract class QuickInsertFunctions
9
{
10
    public static $entityManager;
11
    public static $connection;
12
    protected static $mock = [];
13
    protected static $metadata = [];
14
    protected static $tableName;
15
    protected static $identifier;
16
17
    protected static function buildExtra($extra)
18
    {
19
        $methods = [
20
            'GROUP BY',
21
            'HAVING',
22
            'ORDER BY',
23
        ];
24
        $sql = '';
25
26
        foreach ($methods as $method) {
27
            if (isset($extra[$method])) {
28
                $sql .= ' '.$method.' ';
29
                if (\is_array($extra[$method])) {
30
                    $sql .= implode(' ', $extra[$method]).' ';
31
                } else {
32
                    $sql .= $extra[$method].' ';
33
                }
34
            }
35
        }
36
        Filters::getLimit($extra, $sql);
37
38
        return Helper::oneSpace($sql);
39
    }
40
41
    protected static function buildWhere($tableName, array $where)
42
    {
43
        $whereSql = '';
44
        if (!empty($where)) {
45
            $path = ' WHERE ';
46
            foreach ($where as $key => $value) {
47
                if (!\is_array($value)) {
48
                    if (isset(self::$mock[$tableName][$key])) {
49
                        $whereSql .= $path.self::$mock[$tableName][$key].' = '.self::slashes($tableName, $key, $value);
50
                    } else {
51
                        $whereSql .= $path.$key.' = '.self::slashes($tableName, $key, $value);
52
                    }
53
                } else {
54
                    $whereSql .= $path.$value[0];
55
                }
56
57
                if ($value === reset($where)) {
58
                    $path = ' AND ';
59
                }
60
            }
61
        }
62
63
        return $whereSql;
64
    }
65
66
    protected static function slashes($tableName, $key, $value)
67
    {
68
        if ($value instanceof \DateTime) {
69
            $result = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'";
70
        } else {
71
            $result = self::numeric($tableName, $key, $value) ? (int)$value : "'".addslashes(trim($value))."'";
72
        }
73
74
        $trim = trim($result);
75
        if ($trim === '' || $trim === "''") {
76
            $result = null;
77
        }
78
79
        return $result;
80
    }
81
82
    protected static function numeric($tableName, $key, $value)
83
    {
84
        $intTypes = [
85
            'boolean',
86
            'integer',
87
            'longint',
88
        ];
89
        $flip = [];
90
        if (isset(self::$mock[$tableName])) {
91
            $flip = array_flip(self::$mock[$tableName]);
92
        }
93
94
        if (isset(self::$metadata[$tableName], $flip[$key])) {
95
            if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) {
96
                return true;
97
            }
98
99
            return false;
100
        }
101
102
        return \is_numeric($value) || \is_bool($value);
103
    }
104
105
    protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = [])
106
    {
107
        self::init($manager);
108
        if (\is_object($object)) {
109
            self::extract($object);
110
            $tableName = self::$tableName;
111
            $columns = self::$mock[$tableName] ?: [];
112
            $type = 'object';
113
        } else {
114
            $tableName = $object['table_name'];
115
            unset($object['table_name']);
116
            $type = 'table';
117
            $columns = array_keys($object) ?: [];
118
        }
119
120
        if (isset($extraFields[$tableName])) {
121
            $columns = array_merge($columns, $extraFields[$tableName]);
122
        }
123
    }
124
125
    public static function init($manager = null)
126
    {
127
        if (self::$connection) {
128
            return;
129
        }
130
        global $kernel;
131
132
        if ($kernel instanceof HttpCache) {
133
            $kernel = $kernel->getKernel();
134
        }
135
        $container = $kernel->getContainer();
136
137
        $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager');
138
        if (\is_object($manager)) {
139
            self::$entityManager = $manager;
140
        } else {
141
            self::$entityManager = $container->get('doctrine')->getManager($manager);
142
        }
143
        self::$connection = self::$entityManager->getConnection();
144
    }
145
146
    protected static function extract($object)
147
    {
148
        self::init(false);
149
        $data = Filters::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object)));
150
151
        self::$mock = $data['mock'];
152
        self::$tableName = $data['table'];
153
        self::$metadata[$data['table']] = $data['meta'];
154
        self::$identifier = $data['identifier'];
155
    }
156
157
    protected static function parseUpdateResult($object, $type, $id, $tableName, array $result = null)
158
    {
159
        $data = [];
160
        if (!empty($result)) {
161
            foreach ($result as $key => $value) {
162
                $content = self::value($object, $key, $type, $tableName, false);
163
                if ($id && !\in_array($content, [
164
                        null,
165
                        $value,
166
                    ], true)) {
167
                    $data[$key] = $content;
168
                }
169
            }
170
        }
171
172
        return $data;
173
    }
174
175
    protected static function value($object, $variable, $type, $tableName, $check = true)
176
    {
177
        $value = null;
178
        if ($type === 'object') {
179
            $value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}();
180
        } else {
181
            if (isset($object[$variable])) {
182
                $value = $object[$variable];
183
            }
184
        }
185
186
        if ($check) {
187
            $value = self::slashes($tableName, $variable, $value);
188
        }
189
190
        return $value;
191
    }
192
193
    protected static function makeValues($tableName, array $data = [])
194
    {
195
        $values = '';
196
        if (!empty($data)) {
197
            foreach ($data as $key => $value) {
198
                $values .= self::slashes($tableName, $key, $value).',';
199
            }
200
        }
201
202
        return \substr($values, 0, -1);
203
    }
204
205
    protected static function parsePersistColumns(array $columns = [], $object, $type, $tableName, &$idd)
206
    {
207
        $data = [];
208
        foreach ($columns as $value => $key) {
209
            $keys = [
210
                $key,
211
                $value,
212
            ];
213
            if (!Helper::multiple($keys)) {
214
                $value = self::value($object, $value, $type, $tableName, false);
215
                if ($value !== null) {
216
                    $data[$key] = $value;
217
                    if ($key === self::$identifier) {
218
                        $idd = $value;
219
                    }
220
                }
221
            }
222
        }
223
224
        return $data;
225
    }
226
}
227