Passed
Push — master ( d093e3...81fa39 )
by Dāvis
03:13 queued 25s
created

QuickInsertFunctions   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

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