Passed
Push — main ( e2fa8b...75aa7b )
by Thierry
39:25 queued 27:26
created

DataDump::addRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Facades\Traits;
4
5
use function implode;
6
use function strlen;
7
8
class DataDump
9
{
10
    /**
11
     * @param string $table
12
     * @param int $maxRowSize
13
     * @param string $separator
14
     * @param string $insert
15
     * @param array $dataRows
16
     * @param int $dataSize
17
     * @param string $suffix
18
     */
19
    public function __construct(public string $table, public int $maxRowSize,
20
        public string $separator, public string $insert = '', public array $dataRows = [],
21
        public int $dataSize = 0, public string $suffix = '')
22
    {}
23
24
    /**
25
     * @param array $row
26
     *
27
     * @return void
28
     */
29
    public function addRow(array $row): void
30
    {
31
        $dataRow = '(' . implode(",\t", $row) . ')';
32
        $this->dataRows[] = $dataRow;
33
        $this->dataSize += strlen($dataRow) + 2; // 2 chars for the separator.
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function limitExceeded(): bool
40
    {
41
        // Set a limit to the size of a single INSERT query.
42
        return $this->dataSize + 4 + strlen($this->suffix) >= $this->maxRowSize; // 4 - length specification
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function makeQuery(): string
49
    {
50
        $query = $this->insert . $this->separator .
51
            implode(",$this->separator", $this->dataRows) . $this->suffix;
52
        $this->dataRows = [];
53
        $this->dataSize = 0;
54
55
        return $query;
56
    }
57
}
58