ManagedModelTrait::setReferences()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace LRC\Repository;
4
5
/**
6
 * Foreign-key reference resolution for repository models using repository manager.
7
 */
8
trait ManagedModelTrait
9
{
10
    /**
11
     * @var RepositoryManager   Repository manager.
12
     */
13
    protected $_manager;
14
    
15
    /**
16
     * @var array   Registered references.
17
     */
18
    protected $_references;
19
    
20
    
21
    /**
22
     * Inject a reference to repository manager.
23
     *
24
     * @param RepositoryManager $manager    Repository manager.
25
     *
26
     * @return self
27
     */
28 10
    public function setManager($manager)
29
    {
30 10
        $this->_manager = $manager;
31 10
        return $this;
32
    }
33
    
34
    
35
    /**
36
     * Return registered foreign model references.
37
     *
38
     * @return array    Array of references.
39
     */
40 6
    public function getReferences()
41
    {
42 6
        return ($this->_references ?: []);
43
    }
44
    
45
    
46
    /**
47
     * Register foreign model references.
48
     *
49
     * @param array $references     Array of references (name => config).
50
     *
51
     * @return self
52
     */
53 12
    public function setReferences($references)
54
    {
55 12
        $this->_references = [];
56 12
        foreach ($references as $name => $ref) {
57 12
            if (!array_key_exists('key', $ref)) {
58 9
                $ref['key'] = 'id';
59 9
            }
60 12
            $this->_references[$name] = $ref;
61 12
        }
62 12
        return $this;
63
    }
64
    
65
    
66
    /**
67
     * Retrieve a reference by name.
68
     *
69
     * @param string    $name       Reference name.
70
     *
71
     * @return mixed                Model instance if found, null otherwise.
72
     *
73
     * @throws RepositoryException  If this model or the referenced model is not handled by a managed repository.
74
     */
75 5 View Code Duplication
    public function getReference($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 5
        if (!isset($this->_manager)) {
78 1
            throw new RepositoryException('Model is not handled by a managed repository');
79
        }
80 4
        if (array_key_exists($name, $this->_references)) {
81 4
            $ref = $this->_references[$name];
82 4
            $repo = $this->_manager->getByClass($ref['model']);
83 4
            if (!$repo) {
84 1
                throw new RepositoryException('Referenced model is not handled by a managed repository');
85
            }
86 3
            return ($repo->find($ref['key'], $this->{$ref['attribute']}) ?: null);
87
        }
88 1
        return null;
89
    }
90
    
91
    
92
    /**
93
     * Retrieve a reference by direct attribute access.
94
     *
95
     * @param string            $attr   Attribute name.
96
     *
97
     * @throws RepositoryException      If the requested attribute does not resolve to a registered magic reference.
98
     */
99 7
    public function __get($attr)
100
    {
101 7
        foreach ($this->_references as $name => $ref) {
102 7
            if (!empty($ref['magic']) && $attr === $name) {
103 3
                return $this->getReference($name);
104
            }
105 6
        }
106 6
        throw new RepositoryException("The attribute '$attr' does not resolve to a registered magic reference");
107
    }
108
}
109