Completed
Push — develop ( dfe314...df0194 )
by
unknown
07:26
created

StatusAwareEntityTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setStatus() 0 20 3
A getStatus() 0 4 1
A hasStatus() 0 11 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Entity\Status;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
15
/**
16
 * Trait implementing StatusAwareEntityInterface
17
 * 
18
 * @author Mathias Gelhausen <[email protected]>
19
 * @todo write test 
20
 */
21
trait StatusAwareEntityTrait
22
{
23
    /**
24
     * The status of this entity.
25
     *
26
     * @ODM\EmbedOne(discriminatorField="_entity")
27
     * @var StatusInterface
28
     */
29
    private $status;
30
31
    public function setStatus($state)
32
    {
33
        /** @noinspection PhpUndefinedClassConstantInspection */
34
        $statusClass = static::STATUS_ENTITY_CLASS;
35
36
        if (is_string($state)) {
37
            $state = new $statusClass($state);
38
        }
39
40
        if (!$state instanceOf $statusClass) {
41
            throw new \InvalidArgumentException(sprintf(
42
                'Expected object of type %s, but recieved %s instead.',
43
                $statusClass, get_class($state)
44
            ));
45
        }
46
47
        $this->status = $state;
48
49
        return $this;
50
    }
51
52
    public function getStatus()
53
    {
54
        return $this->status;
55
    }
56
57
    public function hasStatus($state = null)
58
    {
59
        /* @var StatusInterface $status */
60
        $status = $this->getStatus();
61
62
        if (null === $state) {
63
            return is_object($status);
64
        }
65
66
        return $status ? $status->is($state) : false;
67
    }
68
}