Completed
Pull Request — develop (#311)
by
unknown
15:42
created

AttachableEntityManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
     * @param IdentifiableEntityInterface $entity
49
     * @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...
50
     * @return AttachableEntityManager
51
     */
52
    public function setAttachedEntity(IdentifiableEntityInterface $entity, $key = null)
53
    {
54
        $className = get_class($entity);
55
        
56
        if (! isset($key)) {
57
            $key = $className;
58
        }
59
        
60
        $reference = [
61
            'repository' => $className
62
        ];
63
        $entityId = $entity->getId();
64
        
65
        // check if entity is not persisted
66
        if (! $entityId) {
67
            // persist entity & retrieve its ID
68
            $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...
69
            $entityId = $entity->getId();
70
        }
71
        
72
        $reference['id'] = $entityId;
73
        $this->references[$key] = $reference;
74
        
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $key
80
     * @return IdentifiableEntityInterface|null
81
     */
82
    public function getAttachedEntity($key)
83
    {
84
        if (! isset($this->references[$key])) {
85
            return;
86
        }
87
        
88
        $reference = $this->references[$key];
89
        $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...
90
            ->find($reference['id']);
91
        
92
        if (! $entity) {
93
            // remove reference if entity does not exists
94
            unset($this->references[$key]);
95
        }
96
        
97
        return $entity;
98
    }
99
100
    /**
101
     * @param string $key
102
     * @return bool Whether entity with given $key existed
103
     */
104
    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...
105
    {
106
        if (isset($this->references[$key])) {
107
            unset($this->references[$key]);
108
            return true;
109
        }
110
        
111
        return false;
112
    }
113
}
114