Passed
Push — master ( 7c8ae3...3c7bda )
by Dāvis
05:29
created

QuickInsertRepository   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 161
rs 9
c 2
b 0
f 0
wmc 35

9 Methods

Rating   Name   Duplication   Size   Complexity  
A findNextId() 0 6 2
A findNextIdExt() 0 6 1
A runSQL() 0 19 4
A setFK() 0 4 2
C get() 0 30 11
B update() 0 18 5
A link() 0 6 1
C persist() 0 46 8
A delete() 0 6 1
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Repository;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Sludio\HelperBundle\Script\Utils\Helper;
7
8
class QuickInsertRepository extends QuickInsertFunctions
9
{
10
    public static function findNextIdExt(ClassMetadata $metadata, $manager = null)
11
    {
12
        self::init($manager);
13
        $data = self::extractExt($metadata);
14
15
        return self::findNextId($data['table']);
16
    }
17
18
    public static function findNextId($tableName)
19
    {
20
        return self::get(['table_name' => 'information_schema.tables'], true, [
21
            'table_name' => $tableName,
22
            ['table_schema = DATABASE()'],
23
        ], ['AUTO_INCREMENT']) ?: 1;
24
    }
25
26
    public static function get($object, $one = false, array $where = [], array $fields = [], $manager = null, array $extra = [])
27
    {
28
        self::getTable($object, $tableName, $columns, $type, $manager);
29
30
        $select = sprintf('SELECT %s ', isset($extra['MODE']) ? $extra['MODE'] : '');
31
        $fields = $fields ?: ['id'];
0 ignored issues
show
introduced by
The condition $fields can never be true.
Loading history...
32
        $sql = $select.implode(', ', $fields).' FROM '.$tableName.self::buildWhere($tableName, $where).self::buildExtra($extra);
33
34
        $result = self::runSQL($sql) ?: null;
35
36
        if ($result) {
37
            $field = null;
38
            if (\count($fields) === 1 && $fields[0] !== '*') {
39
                $field = $fields[0];
40
            }
41
            if ($field !== null) {
42
                if (!$one) {
43
                    /** @var $result array */
44
                    foreach ($result as &$res) {
45
                        $res = $res[$field];
46
                    }
47
                } else {
48
                    $result = $result[0][$field];
49
                }
50
            } elseif ($one) {
51
                $result = $result[0];
52
            }
53
        }
54
55
        return $result;
56
    }
57
58
    public static function runSQL($sql, $noFkCheck = true, $manager = null, $skip = false)
59
    {
60
        $sql = trim(preg_replace('/\s+/', ' ', $sql));
61
        self::init($manager);
62
        if (!$skip) {
63
            self::setFK(0, $noFkCheck);
64
        }
65
66
        $sth = self::$connection->prepare($sql);
67
        $sth->execute();
68
69
        if (!$skip) {
70
            self::setFK(1, $noFkCheck);
71
        }
72
        if (0 === strpos($sql, 'SELECT')) {
73
            return $sth->fetchAll();
74
        }
75
76
        return true;
77
    }
78
79
    public static function setFK($fkCheck = 0, $noFkCheck = false)
80
    {
81
        if (!$noFkCheck) {
82
            self::runSQL("SET FOREIGN_KEY_CHECKS = $fkCheck", false, null, true);
83
        }
84
    }
85
86
    public static function update($id, $object, array $extraFields = [], $noFkCheck = false, $manager = null)
87
    {
88
        self::getTable($object, $tableName, $columns, $type, $manager, $extraFields);
89
90
        $result = self::get(['table_name' => $tableName], true, ['id' => $id], ['*']) ?: [];
91
        if (isset($result['id'])) {
92
            unset($result['id']);
93
        }
94
        $data = self::parseUpdateResult($object, $type, $id, $tableName, $result);
95
96
        if (!empty($data)) {
97
            $sql = sprintf('UPDATE %s SET ', $tableName);
98
            foreach ($data as $key => $value) {
99
                $sql .= ' '.$key.' = '.self::slashes($tableName, $key, $value).',';
100
            }
101
            $sql = substr($sql, 0, -1).' WHERE id = '.$id;
102
103
            self::runSQL($sql, $noFkCheck);
104
        }
105
    }
106
107
    public static function delete($object, array $where = [], $noFkCheck = false, $manager = null)
108
    {
109
        self::getTable($object, $tableName, $columns, $type, $manager);
110
111
        $sql = sprintf('DELETE FROM %s%s', $tableName, self::buildWhere($tableName, $where));
112
        self::runSQL($sql, $noFkCheck);
113
    }
114
115
    public static function link($object, $data, $noFkCheck = false, $manager = null)
116
    {
117
        self::getTable($object, $tableName, $columns, $type, $manager);
118
119
        $data['table_name'] = $tableName;
120
        self::persist($data, true, [], $noFkCheck, $manager);
121
    }
122
123
    public static function persist($object, $full = false, array $extraFields = [], $noFkCheck = false, $manager = null)
124
    {
125
        self::getTable($object, $tableName, $columns, $type, $manager, $extraFields);
126
127
        $id = self::findNextId($tableName);
128
        $data = [];
129
130
        $idd = null;
131
        /** @var $columns array */
132
        foreach ($columns as $value => $key) {
133
            $keys = [
134
                $key,
135
                $value,
136
            ];
137
            if (!Helper::multiple($keys)) {
138
                $value = self::value($object, $value, $type, $tableName);
139
                if ($value !== null) {
140
                    $data[$key] = $value;
141
                    if ($key === self::$identifier) {
142
                        $idd = $value;
143
                    }
144
                }
145
            }
146
        }
147
148
        if (!$full) {
149
            $data[self::$identifier] = $id;
150
        } else {
151
            $id = $idd;
152
        }
153
154
        if ($id !== null && Helper::isEmpty($data)) {
155
            return null;
156
        }
157
158
        $sql = '
159
            INSERT INTO
160
                '.$tableName.'
161
                    ('.implode(',', array_keys($data)).')
162
            VALUES
163
                ('.self::makeValues($tableName, $data).')
164
        ';
165
166
        self::runSQL($sql, $noFkCheck);
167
168
        return $id;
169
    }
170
}
171