Completed
Push — develop ( 83818f...f24506 )
by
unknown
07:25
created

AttachableEntityTrait::getAttachedEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
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::createAttachedEntity()
80
     */
81
    public function createAttachedEntity($entityClass, $values = [], $key=null)
82
    {
83
        return $this->getAttachableEntityManager()->createAttachedEntity($entityClass, $values, $key);
84
    }
85
    
86
    /**
87
     * @see AttachableEntityInterface::hasAttachedEntity()
88
     */
89
    public function hasAttachedEntity($key)
90
    {
91
        return (bool) $this->getAttachableEntityManager()->getAttachedEntity($key);
92
    }
93
    
94
    /**
95
     * @throws LogicException
96
     * @return AttachableEntityManager
97
     */
98
    protected function getAttachableEntityManager()
99
    {
100
        if (!isset($this->attachableEntityManager)) {
101
            throw new LogicException('No attachable entity manager is set');
102
        }
103
        
104
        return $this->attachableEntityManager;
105
    }
106
}
107