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

AttachableEntityManager::getAttachedEntity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
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
namespace Core\Entity;
10
11
use Core\Repository\RepositoryService;
12
13
/**
14
 *  Manager for attached entities.
15
 *
16
 * @author Miroslav Fedeleš <[email protected]>
17
 * @author Mathias Gelhausen <[email protected]>
18
 * @since 0.28
19
 * @since 0.29 Add ability to create attached entity (which are added automatically)
20
 */
21
class AttachableEntityManager
22
{
23
24
    /**
25
     * @var RepositoryService
26
     */
27
    protected $repositories;
28
29
    /**
30
     * @var array
31
     */
32
    protected $references = [];
33
34
    /**
35
     * @param RepositoryService $repositories
36
     */
37
    public function __construct(RepositoryService $repositories)
38
    {
39
        $this->repositories = $repositories;
40
    }
41
    
42
    /**
43
     * @param array $references
44
     * @return AttachableEntityManager
45
     */
46
    public function setReferences(array & $references)
47
    {
48
        $this->references = & $references;
49
        
50
        return $this;
51
    }
52
53
    /**
54
     * Adds an $entity using an optional $key.
55
     * If $key is not provided then $entity's FQCN will be used as a key
56
     * Any existing $entity with the same $key will be replaced.
57
     *
58
     * @param IdentifiableEntityInterface $entity
59
     * @param string $key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
60
     * @return AttachableEntityManager
61
     */
62
    public function addAttachedEntity(IdentifiableEntityInterface $entity, $key = null)
63
    {
64
        $className = get_class($entity);
65
        
66
        if (! isset($key)) {
67
            $key = $className;
68
        }
69
        
70
        $reference = [
71
            'repository' => $className
72
        ];
73
        $entityId = $entity->getId();
74
        
75
        // check if entity is not persisted
76
        if (! $entityId) {
77
            // persist entity & retrieve its ID
78
            $this->repositories->getRepository($className)->store($entity);
0 ignored issues
show
Documentation Bug introduced by
The method getRepository does not exist on object<Core\Repository\RepositoryService>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
            $entityId = $entity->getId();
80
        }
81
        
82
        $reference['id'] = $entityId;
83
        $this->references[$key] = $reference;
84
        
85
        return $this;
86
    }
87
88
    /**
89
     * Creates an entity and adds it.
90
     *
91
     * @param string        $entityClass
92
     * @param array|string  $values
93
     * @param null|string   $key
94
     *
95
     * @return \Core\Entity\EntityInterface
96
     * @since 0.29
97
     */
98
    public function createAttachedEntity($entityClass, $values = [], $key = null)
99
    {
100
        if (is_string($values)) {
101
            $key = $values;
102
            $values = [];
103
        }
104
105
        $entity = $this->repositories->getRepository($entityClass)->create($values);
0 ignored issues
show
Documentation Bug introduced by
The method getRepository does not exist on object<Core\Repository\RepositoryService>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
106
107
        $this->addAttachedEntity($entity, $key);
108
109
        return $entity;
110
    }
111
112
    /**
113
     * @param string $key
114
     * @return IdentifiableEntityInterface|null
115
     */
116
    public function getAttachedEntity($key)
117
    {
118
        if (! isset($this->references[$key])) {
119
            return;
120
        }
121
        
122
        $reference = $this->references[$key];
123
        $entity = $this->repositories->getRepository($reference['repository'])
0 ignored issues
show
Documentation Bug introduced by
The method getRepository does not exist on object<Core\Repository\RepositoryService>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
124
            ->find($reference['id']);
125
        
126
        if (! $entity) {
127
            // remove reference if entity does not exists
128
            unset($this->references[$key]);
129
        }
130
        
131
        return $entity;
132
    }
133
134
    /**
135
     * @param string $key
136
     * @return bool Whether entity with given $key existed
137
     */
138
    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...
139
    {
140
        if (isset($this->references[$key])) {
141
            unset($this->references[$key]);
142
            return true;
143
        }
144
        
145
        return false;
146
    }
147
}
148