Completed
Push — master ( c2865e...8d4810 )
by Masiukevich
06:56
created

IndexKey::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Event Sourcing implementation
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcing\Indexes;
14
15
use ServiceBus\EventSourcing\Indexes\Exceptions\IndexNameCantBeEmpty;
16
use ServiceBus\EventSourcing\Indexes\Exceptions\ValueKeyCantBeEmpty;
17
18
/**
19
 * The key for the value stored in the index
20
 *
21
 * @property-read string $indexName
22
 * @property-read string $valueKey
23
 */
24
final class IndexKey
25
{
26
    /**
27
     * @var string
28
     */
29
    public $indexName;
30
31
    /**
32
     * @var string
33
     */
34
    public $valueKey;
35
36
    /**
37
     * @param string $indexName
38
     * @param string $valueKey
39
     *
40
     * @return self
41
     *
42
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\IndexNameCantBeEmpty
43
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\ValueKeyCantBeEmpty
44
     */
45 4
    public static function create(string $indexName, string $valueKey): self
46
    {
47 4
        self::assertIndexNameIsNotEmpty($indexName);
48 4
        self::assertValueKeyIsNotEmpty($valueKey);
49
50 4
        return new self($indexName, $valueKey);
51
    }
52
53
    /**
54
     * @param string $indexName
55
     *
56
     * @return void
57
     *
58
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\IndexNameCantBeEmpty
59
     */
60 4
    private static function assertIndexNameIsNotEmpty(string $indexName): void
61
    {
62 4
        if('' === $indexName)
63
        {
64
            throw new IndexNameCantBeEmpty('Index name can\'t be empty');
65
        }
66 4
    }
67
68
    /**
69
     * @param string $valueKey
70
     *
71
     * @return void
72
     *
73
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\ValueKeyCantBeEmpty
74
     */
75 4
    private static function assertValueKeyIsNotEmpty(string $valueKey): void
76
    {
77 4
        if('' === $valueKey)
78
        {
79
            throw new ValueKeyCantBeEmpty('Value key can\'t be empty');
80
        }
81 4
    }
82
83
    /**
84
     * @param string $indexName
85
     * @param string $valueKey
86
     */
87 4
    private function __construct(string $indexName, string $valueKey)
88
    {
89 4
        $this->indexName = $indexName;
0 ignored issues
show
Bug introduced by
The property indexName is declared read-only in ServiceBus\EventSourcing\Indexes\IndexKey.
Loading history...
90 4
        $this->valueKey  = $valueKey;
0 ignored issues
show
Bug introduced by
The property valueKey is declared read-only in ServiceBus\EventSourcing\Indexes\IndexKey.
Loading history...
91 4
    }
92
}
93