Passed
Push — master ( 1c6b85...06020c )
by Dāvis
03:29
created

QuickInsertFunctions   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 165
rs 9
c 0
b 0
f 0
wmc 35

7 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 9 1
A init() 0 15 4
B extractExt() 0 29 5
C buildWhere() 0 22 9
A value() 0 16 4
B getTable() 0 17 5
C buildExtra() 0 34 7
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Repository;
4
5
use Sludio\HelperBundle\Script\Utils\Helper;
6
7
abstract class QuickInsertFunctions
8
{
9
    protected static $mock = [];
10
    protected static $metadata = [];
11
    protected static $tableName;
12
    protected static $identifier;
13
14
    public static $entityManager;
15
    public static $connection;
16
17
    public static function init($manager = null)
18
    {
19
        if (self::$connection) {
20
            return;
21
        }
22
        global $kernel;
23
24
        if ('AppCache' === get_class($kernel)) {
25
            $kernel = $kernel->getKernel();
26
        }
27
        $container = $kernel->getContainer();
28
29
        $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager');
30
        self::$entityManager = $container->get('doctrine')->getManager($manager);
31
        self::$connection = self::$entityManager->getConnection();
32
    }
33
34
    protected static function extract($object)
35
    {
36
        self::init(false);
37
        $data = self::extractExt($object, self::$entityManager);
38
39
        self::$mock = $data['mock'];
40
        self::$tableName = $data['table'];
41
        self::$metadata[$data['table']] = $data['meta'];
42
        self::$identifier = $data['identifier'];
43
    }
44
45
    public static function extractExt($object, $entityManager)
46
    {
47
        $metadata = $entityManager->getClassMetadata(get_class($object));
48
49
        $fields = $metadata->getFieldNames();
50
        $columns = $metadata->getColumnNames();
51
        $table = $metadata->getTableName();
52
        $identifier = null;
53
54
        $result = [];
55
        foreach ($fields as $key => $field) {
56
            foreach ($columns as $key2 => $column) {
57
                if ($key === $key2) {
58
                    $result[$table][$field] = $column;
59
                    if ($field === $metadata->getIdentifier()[0]) {
60
                        $identifier = $column;
61
                    }
62
                }
63
            }
64
        }
65
66
        $data = [
67
            'mock' => $result,
68
            'table' => $table,
69
            'meta' => $metadata,
70
            'identifier' => $identifier,
71
        ];
72
73
        return $data;
74
    }
75
76
    protected static function buildExtra($extra)
77
    {
78
        $methods = [
79
            'GROUP BY',
80
            'HAVING',
81
            'ORDER BY',
82
        ];
83
        $sql = '';
84
85
        foreach ($methods as $method) {
86
            if (isset($extra[$method])) {
87
                $sql .= ' '.$method.' ';
88
                if (is_array($extra[$method])) {
89
                    $sql .= implode(' ', $extra[$method]).' ';
90
                } else {
91
                    $sql .= $extra[$method].' ';
92
                }
93
            }
94
        }
95
96
        if (isset($extra['LIMIT'])) {
97
            if (is_array($extra['LIMIT'])) {
98
                if (isset($extra['LIMIT'][1])) {
99
                    $offset = $extra['LIMIT'][0];
100
                    $limit = $extra['LIMIT'][1];
101
                } else {
102
                    $offset = 0;
103
                    $limit = $extra['LIMIT'][0];
104
                }
105
                $sql .= 'LIMIT '.$offset.', '.$limit;
106
            }
107
        }
108
109
        return Helper::oneSpace($sql);
110
    }
111
112
    protected static function buildWhere($tableName, array $where)
113
    {
114
        $whereSql = '';
115
        if (!empty($where)) {
116
            reset($where);
117
            $first = key($where);
118
            $path = ' WHERE ';
119
            foreach ($where as $key => $value) {
120
                if (!is_array($value) && isset(self::$mock[$tableName][$key])) {
121
                    $whereSql .= $path.self::$mock[$tableName][$key]." = ".(is_numeric($value) ? $value : "'".addslashes(trim($value))."'");
122
                } elseif (is_array($value)) {
123
                    $whereSql .= $path.$value[0];
124
                } else {
125
                    $whereSql .= $path.$key." = ".(is_numeric($value) ? $value : "'".addslashes(trim($value))."'");
126
                }
127
                if ($key === $first) {
128
                    $path = ' AND ';
129
                }
130
            }
131
        }
132
133
        return $whereSql;
134
    }
135
136
    protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, $extraFields = [])
137
    {
138
        self::init($manager);
139
        if (is_object($object)) {
140
            self::extract($object);
141
            $tableName = self::$tableName;
142
            $columns = self::$mock[$tableName] ?: [];
143
            $type = 'object';
144
        } else {
145
            $tableName = $object['table_name'];
146
            unset($object['table_name']);
147
            $type = 'table';
148
            $columns = array_keys($object) ?: [];
149
        }
150
151
        if (isset($extraFields[$tableName])) {
152
            $columns = array_merge($columns, $extraFields[$tableName]);
153
        }
154
    }
155
156
    protected static function value($object, $variable, $type, $check = true)
157
    {
158
        $value = null;
159
        if ($type === 'object') {
160
            $value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}();
161
        } else {
162
            if (isset($object[$variable])) {
163
                $value = $object[$variable];
164
            }
165
        }
166
167
        if ($check) {
168
            Helper::variable($value);
169
        }
170
171
        return $value;
172
    }
173
}