Passed
Push — master ( 33ccda...6d1632 )
by Thorsten
01:57
created

AggregatePrefix::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/cqrs project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\EventSourcing\Aggregate;
12
13
use Assert\Assertion;
14
use Daikon\Entity\ValueObject\ValueObjectInterface;
15
16
final class AggregatePrefix implements AggregateIdInterface
17
{
18
    /** @var string */
19
    private $prefix;
20
21 4
    public static function fromNative($prefix): ValueObjectInterface
22
    {
23 4
        return new static(trim($prefix));
24
    }
25
26 1
    public static function makeEmpty(): ValueObjectInterface
27
    {
28 1
        throw new \Exception("Creating empty aggregate prefixes is not supported.");
29
    }
30
31
    public static function fromFqcn(string $fqcn): ValueObjectInterface
32
    {
33
        Assertion::classExists($fqcn);
34
35
        $parts = explode('\\', $fqcn, 4);
36
        return static::fromNative(
37
            sprintf(
38
                '%s.%s.%s',
39
                static::asSnakeCase($parts[0]),
0 ignored issues
show
Comprehensibility introduced by
Since Daikon\EventSourcing\Aggregate\AggregatePrefix is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
40
                static::asSnakeCase($parts[1]),
0 ignored issues
show
Comprehensibility introduced by
Since Daikon\EventSourcing\Aggregate\AggregatePrefix is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
41
                static::asSnakeCase($parts[2])
0 ignored issues
show
Comprehensibility introduced by
Since Daikon\EventSourcing\Aggregate\AggregatePrefix is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
42
            )
43
        );
44
    }
45
46 2
    public function toNative()
47
    {
48 2
        return $this->prefix;
49
    }
50
51 1
    public function equals(ValueObjectInterface $streamPrefix): bool
52
    {
53 1
        Assertion::isInstanceOf($streamPrefix, static::class);
54 1
        return $this->prefix === $streamPrefix->toNative();
55
    }
56
57 1
    public function isEmpty(): bool
58
    {
59 1
        return false;
60
    }
61
62 1
    public function __toString(): string
63
    {
64 1
        return $this->prefix;
65
    }
66
67
    private static function asSnakeCase(string $value): string
68
    {
69
        return ctype_lower($value)
70
            ? $value
71
            : mb_strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $value));
72
    }
73
74 4
    private function __construct(string $prefix)
75
    {
76 4
        Assertion::notEmpty($prefix);
77 4
        $this->prefix = $prefix;
78 4
    }
79
}
80