Connection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 18.18%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 96
ccs 4
cts 22
cp 0.1818
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bulkInsert() 0 9 2
A getTypo3Service() 0 8 2
A insert() 0 8 1
A update() 0 13 3
1
<?php
2
declare(strict_types = 1);
3
namespace Aoe\AoeDbSequenzer\Xclass;
4
5
/*
6
 * This file xclasses and extends a part of the TYPO3 CMS project.
7
 */
8
9
use Aoe\AoeDbSequenzer\Sequenzer;
10
use Aoe\AoeDbSequenzer\Service\Typo3Service;
11
use TYPO3\CMS\Core\Database\Connection as CoreConnection;
12
use TYPO3\CMS\Core\Database\ConnectionPool;
13
use TYPO3\CMS\Core\Database\Query\BulkInsertQuery;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
/**
17
 * @package Aoe\AoeDbSequenzer\Xclass
18
 */
19
class Connection extends CoreConnection
20
{
21
    /**
22
     * @var Typo3Service
23
     */
24
    private $typo3Service;
25
26
    /**
27
     * Inserts a table row with specified data.
28
     *
29
     * All SQL identifiers are expected to be unquoted and will be quoted when building the query.
30
     * Table expression and columns are not escaped and are not safe for user-input.
31
     *
32
     * @param string $tableName The name of the table to insert data into.
33
     * @param array  $data      An associative array containing column-value pairs.
34
     * @param array  $types     Types of the inserted data.
35
     *
36
     * @return int The number of affected rows.
37
     */
38
    public function insert($tableName, array $data, array $types = []): int
39
    {
40
        return parent::insert(
41
            $tableName,
42
            $this->getTypo3Service()->modifyInsertFields($tableName, $data),
43
            $types
44
        );
45
    }
46
47
    /**
48
     * Bulk inserts table rows with specified data.
49
     *
50
     * All SQL identifiers are expected to be unquoted and will be quoted when building the query.
51
     * Table expression and columns are not escaped and are not safe for user-input.
52
     *
53
     * @param string $tableName The name of the table to insert data into.
54
     * @param array  $data      An array containing associative arrays of column-value pairs.
55
     * @param array  $columns   An array containing associative arrays of column-value pairs.
56
     * @param array  $types     Types of the inserted data.
57
     *
58
     * @return int The number of affected rows.
59
     */
60
    public function bulkInsert(string $tableName, array $data, array $columns = [], array $types = []): int
61
    {
62
        $query = GeneralUtility::makeInstance(BulkInsertQuery::class, $this, $tableName, $columns);
63
        foreach ($data as $values) {
64
            $query->addValues($this->getTypo3Service()->modifyInsertFields($tableName, $values), $types);
65
        }
66
67
        return $query->execute();
68
    }
69
70
    /**
71
     * Executes an SQL UPDATE statement on a table.
72
     *
73
     * All SQL identifiers are expected to be unquoted and will be quoted when building the query.
74
     * Table expression and columns are not escaped and are not safe for user-input.
75
     *
76
     * @param string $tableName  The name of the table to update.
77
     * @param array  $data       An associative array containing column-value pairs.
78
     * @param array  $identifier The update criteria. An associative array containing column-value pairs.
79
     * @param array  $types      Types of the merged $data and $identifier arrays in that order.
80
     *
81
     * @return int The number of affected rows.
82
     */
83
    public function update($tableName, array $data, array $identifier, array $types = []): int
84
    {
85
        if (isset($data['uid']) && $this->getTypo3Service()->needsSequenzer($tableName)) {
86
            throw new \InvalidArgumentException('no uid allowed in update statement!', 1564122222);
87
        }
88
89
        return parent::update(
90
            $tableName,
91
            $data,
92
            $identifier,
93
            $types
94
        );
95
    }
96
97
    /**
98
     * create instance of Typo3Service by lazy-loading
99
     *
100
     * Why we do this?
101
     * Because some unittests backup the variable $GLOBALS (and so, also the variable $GLOBALS['TYPO3_DB']), which means, that this
102
     * object/class will be serialized/unserialized, so the instance of Typo3Service will be null after unserialization!
103
     *
104
     * @return Typo3Service
105
     */
106 1
    protected function getTypo3Service()
107
    {
108 1
        if (false === isset($this->typo3Service)) {
109 1
            $this->typo3Service = GeneralUtility::makeInstance(Typo3Service::class, new Sequenzer());
110
        }
111
112 1
        return $this->typo3Service;
113
    }
114
}
115