Completed
Push — develop ( 6c2664...d15b11 )
by
unknown
07:35
created

AbstractStatusEntity::getStates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license    MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
10
/** */
11
namespace Core\Entity;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
15
/**
16
 * ${CARET}
17
 *
18
 * @ODM\MappedSuperclass
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo   write test
21
 */
22
abstract class AbstractStatusEntity implements StatusInterface
23
{
24
    protected static $orderMap = [];
25
26
    public static function getStates()
27
    {
28
        $map = static::$orderMap;
29
        asort($map, SORT_NUMERIC);
30
31
        return array_keys($map);
32
    }
33
34
    /**
35
     * The default status to be set if none provided.
36
     *
37
     * @var string
38
     */
39
    protected $default;
40
41
    /**
42
     * The status name.
43
     *
44
     * @ODM\Field(type="string")
45
     * @var string
46
     */
47
    private $name;
48
49
    /**
50
     * The order priority
51
     *
52
     * @ODM\Field(type="int")
53
     * @var int
54
     */
55
    private $order;
56
57
    public function __construct($name = null)
58
    {
59
        $this->init($name);
60
    }
61
62
    public function __toString()
63
    {
64
        return $this->name;
65
    }
66
67
    public function is($name)
68
    {
69
        return $this->name == (string) $name;
70
    }
71
72
    /**
73
     * Initialize this state.
74
     *
75
     * @param string $name
76
     *
77
     * @throws \InvalidArgumentException
78
     */
79
    private function init($name)
80
    {
81
        if (null === $name) {
82
            $name = $this->default;
83
        }
84
85
        if (!isset(static::$orderMap[ $name ])) {
86
            throw new \InvalidArgumentException(sprintf(
87
                'Unknown status name "%s" for "%s"',
88
                $name, static::class
89
            ));
90
        }
91
92
        $this->name  = $name;
93
        $this->order = static::$orderMap[ $name ];
94
    }
95
}