Completed
Push — develop ( c603db...77b219 )
by
unknown
16:24 queued 08:23
created

Status::getOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Auth\Entity;
11
12
use Core\Entity\AbstractEntity;
13
use Jobs\Entity\StatusInterface;
14
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
15
use Zend\I18n\Translator\TranslatorInterface as Translator;
16
17
/**
18
 * User status entity
19
 *
20
 * @ODM\EmbeddedDocument
21
 */
22
class Status extends AbstractEntity implements StatusInterface
23
{
24
25
    /**
26
     * status values
27
     */
28
    protected static $orderMap = [
29
        self::ACTIVE => 50,
30
        self::INACTIVE => 60
31
    ];
32
33
    /**
34
     * name of the job status
35
     *
36
     * @var string
37
     * @ODM\Field(type="string")
38
     */
39
    protected $name;
40
41
    /**
42
     * integer for ordering states.
43
     *
44
     * @var int
45
     * @ODM\Field(type="int")
46
     */
47
    protected $order;
48
49
    /**
50
     * @param string $status
51
     * @throws \DomainException
52
     */
53
    public function __construct($status = self::ACTIVE)
54
    {
55
        if (!isset(static::$orderMap[$status])) {
56
            throw new \DomainException('Unknown status: ' . $status);
57
        }
58
        
59
        $constant = 'self::' . strtoupper($status);
60
        $this->name = constant($constant);
61
        $this->order = $this->getOrder();
62
    }
63
64
    /**
65
     * @see \Jobs\Entity\StatusInterface::getName()
66
     * @return String
67
     */
68
    public function getName()
69
    {
70
        return isset($this->name) ? $this->name : '';
71
    }
72
73
    /**
74
     * @see \Jobs\Entity\StatusInterface::getOrder()
75
     * @return Int
76
     */
77
    public function getOrder()
78
    {
79
        return self::$orderMap[$this->getName()];
80
    }
81
82
    public function __toString()
83
    {
84
        return $this->getName();
85
    }
86
87
    public function getStates()
88
    {
89
        $states = self::$orderMap;
90
        asort($states, SORT_NUMERIC);
91
        return array_keys($states);
92
    }
93
    
94
    /**
95
     * @param Translator $translator
96
     * @return array
97
     */
98
    public function getOptions(Translator $translator)
99
    {
100
        $options = [];
101
        
102
        foreach ($this->getStates() as $state)
103
        {
104
            $options[$state] = $translator->translate($state);
105
        }
106
        
107
        return $options;
108
    }
109
}
110