Completed
Push — develop ( b72595...6a420f )
by
unknown
10s
created

setAttachableEntityManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
 * @author Miroslav Fedeleš <[email protected]>
9
 * @since 0.28
10
 */
11
namespace Core\Entity;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
use LogicException;
15
16
/**
17
 * @see AttachableEntityInterface
18
 */
19
trait AttachableEntityTrait
20
{
21
22
    /**
23
     * @var AttachableEntityManager
24
     */
25
    protected $attachableEntityManager;
26
27
    /**
28
     * @var array
29
     * @ODM\Hash
30
     */
31
    protected $attachableEntityReferences = [];
32
    
33
    /**
34
     * @see AttachableEntityInterface::setAttachableEntityManager()
35
     */
36
    public function setAttachableEntityManager(AttachableEntityManager $attachableEntityManager)
37
    {
38
        if (isset($this->attachableEntityManager)) {
39
            throw new LogicException('Attachable entity manager is already set');
40
        }
41
        
42
        $this->attachableEntityManager = $attachableEntityManager->setReferences($this->attachableEntityReferences);
43
        
44
        return $this;
45
    }
46
47
    /**
48
     * @see AttachableEntityInterface::addAttachedEntity()
49
     */
50
    public function addAttachedEntity(IdentifiableEntityInterface $entity, $key = null)
51
    {
52
        $this->getAttachableEntityManager()->addAttachedEntity($entity, $key);
53
        
54
        return $this;
55
    }
56
57
    /**
58
     * @see AttachableEntityInterface::removeAttachedEntity()
59
     */
60
    public function removeAttachedEntity($key)
0 ignored issues
show
Coding Style introduced by
function removeAttachedEntity() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
61
    {
62
        return $this->getAttachableEntityManager()->removeAttachedEntity($key);
63
    }
64
65
    /**
66
     * @see AttachableEntityInterface::getAttachedEntity()
67
     */
68
    public function getAttachedEntity($key = null)
69
    {
70
        if (!isset($key)) {
71
            // allow ommiting parameter for Core\Entity\Hydrator\EntityHydrator::extract()
72
            return;
73
        }
74
        
75
        return $this->getAttachableEntityManager()->getAttachedEntity($key);
76
    }
77
    
78
    /**
79
     * @see AttachableEntityInterface::hasAttachedEntity()
80
     */
81
    public function hasAttachedEntity($key)
82
    {
83
        return (bool) $this->getAttachableEntityManager()->getAttachedEntity($key);
84
    }
85
    
86
    /**
87
     * @throws LogicException
88
     * @return AttachableEntityManager
89
     */
90
    protected function getAttachableEntityManager()
91
    {
92
        if (!isset($this->attachableEntityManager)) {
93
            throw new LogicException('No attachable entity manager is set');
94
        }
95
        
96
        return $this->attachableEntityManager;
97
    }
98
}
99