AbstractState::getTextValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ddd-common.
5
 *
6
 * Copyright 2021 Evgenii Dudal <[email protected]>.
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 * @package ddd-common
11
 */
12
13
namespace RobotE13\DDD\Entities\Status;
14
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * Реализует общие методы интерфейса {{@see PossibleState}}
19
 * @see PossibleStates
20
 * @author Evgenii Dudal <[email protected]>
21
 */
22
abstract class AbstractState implements PossibleStates, RepresentableAsText
23
{
24
25
    private $state;
26
27 3
    public function __construct(int $state)
28
    {
29 3
        $this->setState($state);
30 2
    }
31
32
    /**
33
     * Implements a method from the PossibleState interface.
34
     * @see PossibleState::isValidValue($state)
35
     */
36 3
    final public function checkValidity(int $state): void
37
    {
38 3
        Assert::keyExists($this->getAllExisting(), $state);
39 2
    }
40
41
    /**
42
     * @see PossibleState::getAllExisting()
43
     */
44
    abstract public function getAllExisting(): array;
45
46
    /**
47
     * Implements a method from the PossibleState interface.
48
     * @see PossibleState::isValidValue()
49
     */
50 1
    final public function getTextValue(): string
51
    {
52 1
        return $this->getAllExisting()[$this->state];
53
    }
54
55
    /**
56
     * Returns the state value in a numeric form.
57
     * @return int
58
     */
59 2
    public function getState():int
60
    {
61 2
        return $this->state;
62
    }
63
64 3
    final protected function setState(int $state)
65
    {
66 3
        $this->checkValidity($state);
67 2
        $this->state = $state;
68 2
    }
69
70
}
71