Completed
Branch feature/pre-split (4cb052)
by Anton
03:34
created

RelationBucket   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A extractRelations() 0 10 2
A queueRelations() 0 4 1
A has() 0 4 1
A get() 0 4 1
A set() 0 4 1
A flush() 0 4 1
A __debugInfo() 0 15 4
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Entities;
8
9
use Spiral\ORM\CommandInterface;
10
use Spiral\ORM\ORMInterface;
11
use Spiral\ORM\RecordInterface;
12
use Spiral\ORM\RelationInterface;
13
14
/**
15
 * Represent set of entity relations.
16
 */
17
class RelationBucket
18
{
19
    /**
20
     * @var array|RelationInterface[]
21
     */
22
    private $relations = [];
23
24
    /**
25
     * Relations schema.
26
     *
27
     * @var array
28
     */
29
    private $schema = [];
30
31
    /**
32
     * Associates ORM manager.
33
     *
34
     * @var ORMInterface
35
     */
36
    protected $orm;
37
38
    /**
39
     * @param RecordInterface $record
40
     * @param ORMInterface    $orm
41
     */
42
    public function __construct(RecordInterface $record, ORMInterface $orm)
43
    {
44
        $this->orm = $orm;
45
        $this->schema = $orm->define(get_class($record), ORMInterface::R_RELATIONS);
0 ignored issues
show
Documentation Bug introduced by
It seems like $orm->define(get_class($...Interface::R_RELATIONS) of type * is incompatible with the declared type array of property $schema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
    }
47
48
    /**
49
     * Extract relations data from given entity fields.
50
     *
51
     * @param array $data
52
     */
53
    public function extractRelations(array &$data)
54
    {
55
        //Fetch all relations
56
        $relations = array_intersect_key($data, $this->schema);
57
58
        foreach ($relations as $name => $relation) {
59
            $this->relations[$name] = $relation;
60
            unset($data[$name]);
61
        }
62
    }
63
64
    public function queueRelations(CommandInterface $parent): CommandInterface
65
    {
66
        return $parent;
67
    }
68
69
    /**
70
     * Check if parent entity has associated relation.
71
     *
72
     * @param string $relation
73
     *
74
     * @return bool
75
     */
76
    public function has(string $relation): bool
77
    {
78
        return isset($this->schema[$relation]);
79
    }
80
81
    /**
82
     * Get associated relation instance.
83
     *
84
     * @param string $relation
85
     *
86
     * @return RelationInterface
87
     */
88
    public function get(string $relation): RelationInterface
0 ignored issues
show
Unused Code introduced by
The parameter $relation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
91
    }
92
93
    public function set(string $relation, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $relation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
96
    }
97
98
    public function flush(string $relation)
0 ignored issues
show
Unused Code introduced by
The parameter $relation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
101
    }
102
103
    /**
104
     * Information about loaded relations.
105
     *
106
     * @return array
107
     */
108
    public function __debugInfo()
109
    {
110
        $relations = [];
111
112
        foreach ($this->schema as $name => $content) {
113
            if (!array_key_exists($name, $this->relations)) {
114
                $relations[$name] = 'none';
115
                continue;
116
            }
117
118
            $relations[$name] = empty($this->relations[$name]) ? 'empty' : 'loaded';
119
        }
120
121
        return $relations;
122
    }
123
}