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

AttachableEntityManager::setReferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 Core\Repository\RepositoryService;
14
15
class AttachableEntityManager
16
{
17
18
    /**
19
     * @var RepositoryService
20
     */
21
    protected $repositories;
22
23
    /**
24
     * @var array
25
     */
26
    protected $references = [];
27
28
    /**
29
     * @param RepositoryService $repositories
30
     */
31
    public function __construct(RepositoryService $repositories)
32
    {
33
        $this->repositories = $repositories;
34
    }
35
    
36
    /**
37
     * @param array $references
38
     * @return AttachableEntityManager
39
     */
40
    public function setReferences(array & $references)
41
    {
42
        $this->references = & $references;
43
        
44
        return $this;
45
    }
46
47
    /**
48
     * Adds an $entity using an optional $key.
49
     * If $key is not provided then $entity's FQCN will be used as a key
50
     * Any existing $entity with the same $key will be replaced.
51
     *
52
     * @param IdentifiableEntityInterface $entity
53
     * @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...
54
     * @return AttachableEntityManager
55
     */
56
    public function addAttachedEntity(IdentifiableEntityInterface $entity, $key = null)
57
    {
58
        $className = get_class($entity);
59
        
60
        if (! isset($key)) {
61
            $key = $className;
62
        }
63
        
64
        $reference = [
65
            'repository' => $className
66
        ];
67
        $entityId = $entity->getId();
68
        
69
        // check if entity is not persisted
70
        if (! $entityId) {
71
            // persist entity & retrieve its ID
72
            $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...
73
            $entityId = $entity->getId();
74
        }
75
        
76
        $reference['id'] = $entityId;
77
        $this->references[$key] = $reference;
78
        
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $key
84
     * @return IdentifiableEntityInterface|null
85
     */
86
    public function getAttachedEntity($key)
87
    {
88
        if (! isset($this->references[$key])) {
89
            return;
90
        }
91
        
92
        $reference = $this->references[$key];
93
        $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...
94
            ->find($reference['id']);
95
        
96
        if (! $entity) {
97
            // remove reference if entity does not exists
98
            unset($this->references[$key]);
99
        }
100
        
101
        return $entity;
102
    }
103
104
    /**
105
     * @param string $key
106
     * @return bool Whether entity with given $key existed
107
     */
108
    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...
109
    {
110
        if (isset($this->references[$key])) {
111
            unset($this->references[$key]);
112
            return true;
113
        }
114
        
115
        return false;
116
    }
117
}
118