Completed
Push — v4.0 ( 989be1...ff495d )
by Masiukevich
02:21
created

IndexValue::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 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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\EmptyValuesNotAllowed;
16
use ServiceBus\EventSourcing\Indexes\Exceptions\InvalidValueType;
17
18
/**
19
 * The value stored in the index.
20
 *
21
 * @psalm-readonly
22
 */
23
final class IndexValue
24
{
25
    /**
26
     * @var mixed
27
     */
28
    public $value;
29
30
    /**
31
     * @param mixed $value
32
     *
33
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\InvalidValueType
34
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\EmptyValuesNotAllowed
35
     */
36 7
    public function __construct($value)
37
    {
38 7
        self::assertIsScalar($value);
39 6
        self::assertNotEmpty($value);
40
41 5
        $this->value = $value;
42 5
    }
43
44
    /**
45
     * @param mixed $value
46
     *
47
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\EmptyValuesNotAllowed
48
     */
49 6
    private static function assertNotEmpty($value): void
50
    {
51 6
        if ('' === (string) $value)
52
        {
53 1
            throw new EmptyValuesNotAllowed('Value can not be empty');
54
        }
55 5
    }
56
57
    /**
58
     * @param mixed $value
59
     *
60
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\InvalidValueType
61
     */
62 7
    private static function assertIsScalar($value): void
63
    {
64 7
        if (false === \is_scalar($value))
65
        {
66 1
            throw new InvalidValueType(
67 1
                \sprintf('The value must be of type "scalar". "%s" passed', \gettype($value))
68
            );
69
        }
70 6
    }
71
}
72