Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
24 / 28 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SupplierDocument | |
85.71% |
24 / 28 |
|
40.00% |
2 / 5 |
17.84 | |
0.00% |
0 / 1 |
| normalize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isValid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeFromImport | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| legacySearchVariant | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
5.27 | |||
| shouldReplaceStoredDocument | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Supplier\Support; |
| 4 | |
| 5 | final class SupplierDocument |
| 6 | { |
| 7 | public static function normalize(?string $document): ?string |
| 8 | { |
| 9 | if ($document === null) { |
| 10 | return null; |
| 11 | } |
| 12 | |
| 13 | return preg_replace('/\D+/', '', $document); |
| 14 | } |
| 15 | |
| 16 | public static function isValid(?string $document): bool |
| 17 | { |
| 18 | $normalized = self::normalize($document); |
| 19 | |
| 20 | return in_array(strlen($normalized ?? ''), [11, 14], true); |
| 21 | } |
| 22 | |
| 23 | public static function normalizeFromImport(?string $document): ?string |
| 24 | { |
| 25 | $normalized = self::normalize($document); |
| 26 | |
| 27 | if ($normalized === null) { |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | $length = strlen($normalized); |
| 32 | |
| 33 | if ($length === 10 || $length === 13) { |
| 34 | return '0' . $normalized; |
| 35 | } |
| 36 | |
| 37 | return $normalized; |
| 38 | } |
| 39 | |
| 40 | public static function legacySearchVariant(?string $document): ?string |
| 41 | { |
| 42 | $normalized = self::normalize($document); |
| 43 | |
| 44 | if ($normalized === null) { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | if (! str_starts_with($normalized, '0')) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | $length = strlen($normalized); |
| 53 | |
| 54 | if ($length !== 11 && $length !== 14) { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | return substr($normalized, 1); |
| 59 | } |
| 60 | |
| 61 | public static function shouldReplaceStoredDocument(?string $storedDocument, ?string $importedDocument): bool |
| 62 | { |
| 63 | $stored = self::normalize($storedDocument); |
| 64 | $imported = self::normalizeFromImport($importedDocument); |
| 65 | |
| 66 | if ($stored === null || $imported === null) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return ! self::isValid($stored) |
| 71 | && self::isValid($imported) |
| 72 | && $stored !== $imported; |
| 73 | } |
| 74 | } |