UuidStrategy   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 67
ccs 26
cts 34
cp 0.7647
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailableVersions() 0 12 2
A parseVersions() 0 19 5
A generateArguments() 0 9 1
A getAttributeClassName() 0 3 1
A getValidatorProperty() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Preparation\Processor\Property\Assert;
15
16
use Symfony\Component\Validator\Constraints\Uuid;
17
18
class UuidStrategy extends AbstractConstraintStrategy
19
{
20
    /**
21
     * @return array<int>
22
     */
23 1
    public function getAvailableVersions(): array
24
    {
25
        // Supports 5.4 version
26 1
        $constants = [
27 1
            'V1_MAC', 'V2_DCE', 'V3_MD5', 'V4_RANDOM', 'V5_SHA1', 'V6_SORTABLE', 'V7_MONOTONIC',
28 1
        ];
29
30 1
        return array_filter(array_map(function (string $constName) {
31 1
            $cName = sprintf('%s::%s', Uuid::class, $constName);
32
33 1
            return \defined($cName) ? \constant($cName) : null;
34 1
        }, $constants));
35
    }
36
37 1
    protected function generateArguments(array $config): array
38
    {
39
        /** @psalm-suppress PossiblyInvalidArgument */
40 1
        return array_filter(
41 1
            array_merge(
42 1
                parent::generateArguments($config),
43 1
                [
44 1
                    'versions' => $this->parseVersions(trim($config['versions'] ?? '')),
45 1
                    'strict' => $this->stringToBool($config['strict'] ?? 'true'),
46 1
                ]
47 1
            )
48 1
        );
49
    }
50
51 1
    protected function getValidatorProperty(): string
52
    {
53 1
        return 'uuid';
54
    }
55
56 1
    protected function getAttributeClassName(): string
57
    {
58 1
        return Uuid::class;
59
    }
60
61
    /**
62
     * @param array<int|string>|string $original
63
     *
64
     * @return array<int>
65
     */
66 1
    private function parseVersions(array|string $original): array
67
    {
68 1
        $availableVersions = $this->getAvailableVersions();
69 1
        if ('' === $original) {
0 ignored issues
show
introduced by
The condition '' === $original is always false.
Loading history...
70 1
            return $availableVersions;
71
        }
72
73
        $versions = $original;
74
        if (\is_string($versions)) {
0 ignored issues
show
introduced by
The condition is_string($versions) is always false.
Loading history...
75
            $versions = $this->explodeString($versions);
76
        }
77
78
        $versions = array_map('intval', $versions);
79
        $isValid = !array_diff($versions, $availableVersions);
80
        if ($isValid) {
81
            return $versions;
82
        }
83
84
        throw new \InvalidArgumentException(sprintf('UUID versions is not allowed. Actual: `%s`, Available: `%s`', \is_array($original) ? implode(', ', $original) : $original, implode(',', $availableVersions)));
0 ignored issues
show
introduced by
The condition is_array($original) is always true.
Loading history...
85
    }
86
}
87