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

IndexValue::assertNotEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
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\EmptyValuesNotAllowed;
16
use ServiceBus\EventSourcing\Indexes\Exceptions\InvalidValueType;
17
18
/**
19
 * The value stored in the index
20
 *
21
 * @property-read mixed $value
22
 */
23
final class IndexValue
24
{
25
    /**
26
     * @var mixed
27
     */
28
    public $value;
29
30
    /**
31
     * @param mixed $value
32
     *
33
     * @return self
34
     *
35
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\InvalidValueType
36
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\EmptyValuesNotAllowed
37
     */
38 7
    public static function create($value): self
39
    {
40 7
        self::assertIsScalar($value);
41 6
        self::assertNotEmpty($value);
42
43 5
        return new self($value);
44
    }
45
46
    /**
47
     * @param mixed $value
48
     *
49
     * @return void
50
     *
51
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\EmptyValuesNotAllowed
52
     */
53 6
    private static function assertNotEmpty($value): void
54
    {
55 6
        if('' === (string) $value)
56
        {
57 1
            throw new EmptyValuesNotAllowed('Value can not be empty');
58
        }
59 5
    }
60
61
    /**
62
     * @param mixed $value
63
     *
64
     * @return void
65
     *
66
     * @throws \ServiceBus\EventSourcing\Indexes\Exceptions\InvalidValueType
67
     */
68 7
    private static function assertIsScalar($value): void
69
    {
70 7
        if(false === \is_scalar($value))
71
        {
72 1
            throw new InvalidValueType(
73 1
                \sprintf('The value must be of type "scalar". "%s" passed', \gettype($value))
74
            );
75
        }
76 6
    }
77
78
    /**
79
     * @param mixed $value
80
     */
81 5
    private function __construct($value)
82
    {
83 5
        $this->value = $value;
0 ignored issues
show
Bug introduced by
The property value is declared read-only in ServiceBus\EventSourcing\Indexes\IndexValue.
Loading history...
84 5
    }
85
}
86