Passed
Push — master ( 7ce289...8aaaed )
by Dāvis
04:53
created

QuickInsertRepository::persist()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 5
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
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'];
32
        $sql = $select.implode(', ', $fields).' FROM '.$tableName.self::buildWhere($tableName, $where).self::buildExtra($extra);
33
34
        return self::filterGetResult((self::runSQL($sql) ?: null), $fields, $one);
0 ignored issues
show
Bug introduced by
It seems like self::runSQL($sql) ?: null can also be of type true; however, parameter $result of Sludio\HelperBundle\Scri...ions::filterGetResult() does only seem to accept null|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return self::filterGetResult(/** @scrutinizer ignore-type */ (self::runSQL($sql) ?: null), $fields, $one);
Loading history...
35
    }
36
37
    public static function runSQL($sql, $noFkCheck = true, $manager = null, $skip = false)
38
    {
39
        $sql = trim(preg_replace('/\s+/', ' ', $sql));
40
        self::init($manager);
41
        if (!$skip) {
42
            self::setFK(0, $noFkCheck);
43
        }
44
45
        $sth = self::$connection->prepare($sql);
46
        $sth->execute();
47
48
        if (!$skip) {
49
            self::setFK(1, $noFkCheck);
50
        }
51
        if (0 === strpos($sql, 'SELECT')) {
52
            return $sth->fetchAll();
53
        }
54
55
        return true;
56
    }
57
58
    public static function setFK($fkCheck = 0, $noFkCheck = false)
59
    {
60
        if (!$noFkCheck) {
61
            self::runSQL("SET FOREIGN_KEY_CHECKS = $fkCheck", false, null, true);
62
        }
63
    }
64
65
    public static function update($id, $object, array $extraFields = [], $noFkCheck = false, $manager = null)
66
    {
67
        self::getTable($object, $tableName, $columns, $type, $manager, $extraFields);
68
69
        $result = self::get(['table_name' => $tableName], true, ['id' => $id], ['*']) ?: [];
70
        if (isset($result['id'])) {
71
            unset($result['id']);
72
        }
73
        $data = self::parseUpdateResult($object, $type, $id, $tableName, $result);
74
75
        if (!empty($data)) {
76
            $sql = sprintf('UPDATE %s SET ', $tableName);
77
            foreach ($data as $key => $value) {
78
                $sql .= ' '.$key.' = '.self::slashes($tableName, $key, $value).',';
79
            }
80
            $sql = substr($sql, 0, -1).' WHERE id = '.$id;
81
82
            self::runSQL($sql, $noFkCheck);
83
        }
84
    }
85
86
    public static function delete($object, array $where = [], $noFkCheck = false, $manager = null)
87
    {
88
        self::getTable($object, $tableName, $columns, $type, $manager);
89
90
        $sql = sprintf('DELETE FROM %s%s', $tableName, self::buildWhere($tableName, $where));
91
        self::runSQL($sql, $noFkCheck);
92
    }
93
94
    public static function link($object, $data, $noFkCheck = false, $manager = null)
95
    {
96
        self::getTable($object, $tableName, $columns, $type, $manager);
97
98
        $data['table_name'] = $tableName;
99
        self::persist($data, true, [], $noFkCheck, $manager);
100
    }
101
102
    public static function persist($object, $full = false, array $extraFields = [], $noFkCheck = false, $manager = null)
103
    {
104
        self::getTable($object, $tableName, $columns, $type, $manager, $extraFields);
105
106
        $id = self::findNextId($tableName);
107
        $data = self::parsePersistColumns($columns, $object, $type, $tableName, $idd);
108
109
        if (!$full) {
110
            $data[self::$identifier] = $id;
111
        } else {
112
            $id = $idd;
113
        }
114
115
        if ($id !== null && Helper::isEmpty($data)) {
116
            return null;
117
        }
118
119
        $sql = '
120
            INSERT INTO
121
                '.$tableName.'
122
                    ('.implode(',', array_keys($data)).')
123
            VALUES
124
                ('.self::makeValues($tableName, $data).')
125
        ';
126
127
        self::runSQL($sql, $noFkCheck);
128
129
        return $id;
130
    }
131
}
132