Completed
Push — develop ( 6c2664...d15b11 )
by
unknown
15:07 queued 07:36
created

StatusAwareEntityTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 8 2
A setStatus() 0 10 2
A hasStatus() 0 4 1
A createStatusFromName() 0 8 2
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
/**
14
 * Implementation of the StatusAwareEntityInterface.
15
 *
16
 * @property-read string $statusEntity FQCN of the Status entity class.
17
 * @property-read string|StatusInterface $statusDefault Default status entity or name,
18
 * @author Mathias Gelhausen <[email protected]>
19
 * @todo write test 
20
 */
21
trait StatusAwareEntityTrait
22
{
23
24
    /**
25
     * The status entity.
26
     *
27
     * @var StatusInterface
28
     */
29
    private $status;
30
31
    public function getStatus()
32
    {
33
        if (!$this->status) {
34
            $this->setStatus(null);
35
        }
36
37
        return $this->status;
38
    }
39
40
    public function setStatus($status)
41
    {
42
        if (!$status instanceOf StatusInterface) {
43
            $status = $this->createStatusFromName($status);
44
        }
45
46
        $this->status = $status;
47
48
        return $this;
49
    }
50
51
    public function hasStatus($status)
52
    {
53
        return $this->getStatus()->is($status);
54
    }
55
56
    /**
57
     * Create a status from the status name.
58
     *
59
     * @param string|null $status
60
     *
61
     * @return StatusInterface
62
     * @throws \RuntimeException if the property $statusEntity is not defined.
63
     */
64
    private function createStatusFromName($status = null)
65
    {
66
        if (!property_exists($this, 'statusEntity')) {
67
            throw new \RuntimeException('No status entity defined.');
68
        }
69
70
        return new $this->statusEntity($status);
71
    }
72
}