GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#192)
by joseph
40:19
created

BulkSimpleEntityCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories\UuidFactory;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntityUpdater\BulkSimpleEntityCreatorHelper;
10
use EdmondsCommerce\DoctrineStaticMeta\Schema\MysqliConnectionFactory;
11
use EdmondsCommerce\DoctrineStaticMeta\Schema\UuidFunctionPolyfill;
12
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
13
use Ramsey\Uuid\UuidInterface;
14
15
/**
16
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
 */
18
class BulkSimpleEntityCreator extends AbstractBulkProcess
19
{
20
    public const INSERT_MODE_INSERT  = 'INSERT ';
21
    public const INSERT_MODE_IGNORE  = 'INSERT IGNORE ';
22
    public const INSERT_MODE_DEFAULT = self::INSERT_MODE_INSERT;
23
    public const INSERT_MODES        = [
24
        self::INSERT_MODE_INSERT,
25
        self::INSERT_MODE_IGNORE,
26
    ];
27
28
    /**
29
     * @var BulkSimpleEntityCreatorHelper
30
     */
31
    private $helper;
32
    /**
33
     * @var string
34
     */
35
    private $tableName;
36
    /**
37
     * @var string
38
     */
39
    private $entityFqn;
40
    /**
41
     * Is the UUID binary
42
     *
43
     * @var bool
44
     */
45
    private $isBinaryUuid = true;
46
    /**
47
     * @var ClassMetadataInfo
48
     */
49
    private $meta;
50
    /**
51
     * @var string
52
     */
53
    private $primaryKeyCol;
54
    /**
55
     * @var \mysqli
56
     */
57
    private $mysqli;
58
    /**
59
     * @var UuidFunctionPolyfill
60
     */
61
    private $uuidFunctionPolyfill;
62
    /**
63
     * @var UuidFactory
64
     */
65
    private $uuidFactory;
66
    /**
67
     * @var string
68
     */
69
    private $query;
70
    /**
71
     * For creation this should always be 100%, so 1
72
     *
73
     * @var int
74
     */
75
    private $requireAffectedRatio = 1;
76
    /**
77
     * @var int
78
     */
79
    private $totalAffectedRows = 0;
80
81
    private $insertMode = self::INSERT_MODE_DEFAULT;
82
83 1
    public function __construct(
84
        EntityManagerInterface $entityManager,
85
        MysqliConnectionFactory $mysqliConnectionFactory,
86
        UuidFunctionPolyfill $uuidFunctionPolyfill,
87
        UuidFactory $uuidFactory
88
    ) {
89 1
        parent::__construct($entityManager);
90 1
        $this->mysqli               = $mysqliConnectionFactory->createFromEntityManager($entityManager);
91 1
        $this->uuidFunctionPolyfill = $uuidFunctionPolyfill;
92 1
        $this->uuidFactory          = $uuidFactory;
93 1
    }
94
95 1
    public function endBulkProcess(): void
96
    {
97 1
        parent::endBulkProcess();
98
        // Reset the insert mode to default to prevent state bleeding across batch runs
99 1
        $this->setInsertMode(self::INSERT_MODE_DEFAULT);
100
101 1
    }
102
103
    /**
104
     * @param string $insertMode
105
     *
106
     * @return BulkSimpleEntityCreator
107
     */
108 1
    public function setInsertMode(string $insertMode): BulkSimpleEntityCreator
109
    {
110 1
        if (false === \in_array($insertMode, self::INSERT_MODES, true)) {
111
            throw new \InvalidArgumentException('Invalid insert mode');
112
        }
113 1
        $this->insertMode = $insertMode;
114 1
        if ($this->insertMode === self::INSERT_MODE_IGNORE) {
115
            $this->requireAffectedRatio = 0;
116
        }
117
118 1
        return $this;
119
    }
120
121
    public function addEntityToSave(EntityInterface $entity): void
122
    {
123
        throw new \RuntimeException('You should not try to save Entities with this saver');
124
    }
125
126 1
    public function addEntitiesToSave(array $entities): void
127
    {
128 1
        foreach ($entities as $entityData) {
129 1
            if (\is_array($entityData)) {
130 1
                $this->addEntityCreationData($entityData);
131 1
                continue;
132
            }
133
            throw new \InvalidArgumentException('You should only pass in simple arrays of scalar entity data');
134
        }
135 1
    }
136
137 1
    public function addEntityCreationData(array $entityData): void
138
    {
139 1
        $this->entitiesToSave[] = $entityData;
140 1
        $this->bulkSaveIfChunkBigEnough();
141 1
    }
142
143 1
    public function setHelper(BulkSimpleEntityCreatorHelper $helper): void
144
    {
145 1
        $this->helper        = $helper;
146 1
        $this->tableName     = $helper->getTableName();
147 1
        $this->entityFqn     = $helper->getEntityFqn();
148 1
        $this->meta          = $this->entityManager->getClassMetadata($this->entityFqn);
149 1
        $this->primaryKeyCol = $this->meta->getSingleIdentifierFieldName();
150 1
        $this->isBinaryUuid  = $this->isBinaryUuid();
151 1
        $this->runPolyfillIfRequired();
152 1
    }
153
154 1
    private function isBinaryUuid(): bool
155
    {
156 1
        $idMapping = $this->meta->getFieldMapping($this->meta->getSingleIdentifierFieldName());
157
158 1
        return $idMapping['type'] === UuidBinaryOrderedTimeType::NAME;
159
    }
160
161 1
    private function runPolyfillIfRequired(): void
162
    {
163 1
        if (false === $this->isBinaryUuid) {
164
            return;
165
        }
166 1
        $this->uuidFunctionPolyfill->run();
167 1
    }
168
169
    /**
170
     * As these are not actually entities, lets empty them out before
171
     * parent::freeResources tries to detach from the entity manager
172
     */
173 1
    protected function freeResources()
174
    {
175 1
        $this->entitiesToSave = [];
176 1
        parent::freeResources();
177 1
    }
178
179 1
    protected function doSave(): void
180
    {
181 1
        foreach ($this->entitiesToSave as $entityData) {
182 1
            $this->appendToQuery($this->buildSql($entityData));
183
        }
184 1
        $this->runQuery();
185 1
        $this->reset();
186 1
    }
187
188 1
    private function appendToQuery(string $sql)
189
    {
190 1
        $this->query .= "\n$sql";
191 1
    }
192
193 1
    private function buildSql(array $entityData): string
194
    {
195 1
        $sql  = $this->insertMode . " into {$this->tableName} set ";
196
        $sqls = [
197 1
            $this->primaryKeyCol . ' = ' . $this->generateId(),
198
        ];
199 1
        foreach ($entityData as $key => $value) {
200 1
            if ($key === $this->primaryKeyCol) {
201
                throw new \InvalidArgumentException(
202
                    'You should not pass in IDs, they will be auto generated'
203
                );
204
            }
205 1
            if ($value instanceof UuidInterface) {
206
                $sqls[] = "`$key` = " . $this->getUuidSql($value);
207
                continue;
208
            }
209 1
            $value  = $this->mysqli->escape_string((string)$value);
210 1
            $sqls[] = "`$key` = '$value'";
211
        }
212 1
        $sql .= implode(', ', $sqls) . ';';
213
214 1
        return $sql;
215
    }
216
217 1
    private function generateId()
218
    {
219 1
        if ($this->isBinaryUuid) {
220 1
            return $this->getUuidSql($this->uuidFactory->getOrderedTimeUuid());
221
        }
222
223
        return $this->getUuidSql($this->uuidFactory->getUuid());
224
    }
225
226 1
    private function getUuidSql(UuidInterface $uuid)
227
    {
228 1
        if ($this->isBinaryUuid) {
229 1
            $uuidString = (string)$uuid;
230 1
            return "UUID_TO_BIN('$uuidString', true)";
231
        }
232
233
        throw new \RuntimeException('This is not currently suppported - should be easy enough though');
234
    }
235
236 1
    private function runQuery(): void
237
    {
238 1
        if ('' === $this->query) {
239
            return;
240
        }
241 1
        $this->query = "
242
           START TRANSACTION;
243
           SET FOREIGN_KEY_CHECKS = 0; 
244 1
           {$this->query}             
245
           SET FOREIGN_KEY_CHECKS = 1; 
246
           COMMIT;";
247 1
        $result      = $this->mysqli->multi_query($this->query);
248 1
        if (true !== $result) {
249
            throw new \RuntimeException(
250
                'Multi Query returned false which means the first statement failed: ' .
251
                $this->mysqli->error
252
            );
253
        }
254 1
        $affectedRows = 0;
255 1
        $queryCount   = 0;
256
        do {
257 1
            $queryCount++;
258 1
            $errorNo = (int)$this->mysqli->errno;
259 1
            if (0 !== $errorNo) {
260
                $errorMessage = 'Query #' . $queryCount .
261
                                ' got MySQL Error #' . $errorNo .
262
                                ': ' . $this->mysqli->error
263
                                . "\nQuery: " . $this->getQueryLine($queryCount) . "'\n";
264
                throw new \RuntimeException($errorMessage);
265
            }
266 1
            $affectedRows += max($this->mysqli->affected_rows, 0);
267 1
            if (false === $this->mysqli->more_results()) {
268 1
                break;
269
            }
270 1
            $this->mysqli->next_result();
271 1
        } while (true);
272 1
        if ($affectedRows < count($this->entitiesToSave) * $this->requireAffectedRatio) {
273
            throw new \RuntimeException(
274
                'Affected rows count of ' . $affectedRows .
275
                ' does match the expected count of entitiesToSave ' . count($this->entitiesToSave)
276
            );
277
        }
278 1
        $this->totalAffectedRows += $affectedRows;
279 1
        $this->mysqli->commit();
280 1
    }
281
282
    private function getQueryLine(int $line): string
283
    {
284
        $lines = explode(";\n", $this->query);
285
286
        return $lines[$line + 1];
287
    }
288
289 1
    private function reset(): void
290
    {
291 1
        $this->entitiesToSave = [];
292 1
        $this->query          = '';
293 1
    }
294
}
295