BinaryUuidSupport::transformUuidsToBytes()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
1
<?php
2
declare(strict_types=1);
3
4
namespace ReadModel\Bridge\Doctrine\Query;
5
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Ramsey\Uuid\Exception\InvalidUuidStringException;
8
use Ramsey\Uuid\Uuid;
9
use ReadModel\Walker\WalkerBuilder;
10
11
trait BinaryUuidSupport
12
{
13
    protected function transformParameters(QueryBuilder $qb, string ...$parameters): void
14
    {
15
        $this->transformUuidsToBytes($qb, $parameters);
16
    }
17
18
    protected function createWalkerBuilder(array $uuids): WalkerBuilder
19
    {
20
        $uuids = array_merge(['id'], $uuids);
21
22
        return parent::createWalkerBuilder($uuids)->withBinaryUuidCasting(...$uuids);
23
    }
24
25
    private function transformUuidsToBytes(QueryBuilder $qb, array $parameters): void
26
    {
27
        $parameters = array_merge(['id'], $parameters);
28
29
        foreach ($qb->getParameters() as $key => $value) {
30
            if (empty($value) || !in_array($key, $parameters)) {
31
                continue;
32
            }
33
34
            try {
35
                $value = Uuid::fromString($value)->getBytes();
36
                $qb->setParameter($key, $value);
37
            } catch (InvalidUuidStringException $e) {
38
                // just skip it
39
            }
40
        }
41
    }
42
}
43