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

AggregatePrefix   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 56.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 64
ccs 17
cts 30
cp 0.5667
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 4 1
A makeEmpty() 0 4 1
A fromFqcn() 0 14 1
A toNative() 0 4 1
A equals() 0 5 1
A isEmpty() 0 4 1
A __toString() 0 4 1
A asSnakeCase() 0 6 2
A __construct() 0 5 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