Completed
Push — master ( 2d3c47...157dd8 )
by Chris
03:55
created

HasMany::eagerSelf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Darya\ORM\Relation;
3
4
use Darya\ORM\Record;
5
use Darya\ORM\Relation;
6
7
/**
8
 * Darya's has-many entity relation.
9
 * 
10
 * @author Chris Andrew <[email protected]>
11
 */
12
class HasMany extends Has
13
{
14
	/**
15
	 * Eagerly load the related models of the given parent instances.
16
	 * 
17
	 * Retrieves the related models without matching them to their parents.
18
	 * 
19
	 * @param array $instances
20
	 * @return array
21
	 */
22
	public function eagerLoad(array $instances)
23
	{
24
		$this->verifyParents($instances);
25
		$ids = static::attributeList($instances, 'id');
26
		
27
		$filter = array_merge($this->filter(), array(
28
			$this->foreignKey => array_unique($ids)
29
		));
30
		
31
		$data = $this->storage()->read($this->target->table(), $filter, $this->order());
32
		
33
		return $this->target->generate($data);
34
	}
35
	
36
	/**
37
	 * Eagerly load and match the related models for the given parent instances.
38
	 * 
39
	 * Returns the given instances with their related models loaded.
40
	 * 
41
	 * @param array $instances
42
	 * @return array
43
	 */
44
	public function eager(array $instances)
45
	{
46
		if ($this->parent instanceof $this->target) {
47
			return $this->eagerSelf($instances);
48
		}
49
		
50
		$related = $this->eagerLoad($instances);
51
		
52
		$instances = $this->match($instances, $related);
53
		
54
		return $instances;
55
	}
56
	
57
	/**
58
	 * Eagerly load the related models from the same table.
59
	 * 
60
	 * This continues to load the same relation recursively.
61
	 * 
62
	 * @param array $instances
63
	 * @return array
64
	 */
65
	protected function eagerSelf(array $instances)
66
	{
67
		$parents = $instances;
68
		
69
		while ($related = $this->eagerLoad($parents)) {
70
			$this->match($parents, $related);
71
			
72
			$parents = $related;
73
		}
74
		
75
		$this->match($parents, array());
76
		
77
		return $instances;
78
	}
79
	
80
	/**
81
	 * Match the given related models to their parent instances.
82
	 * 
83
	 * @param Record[] $instances
84
	 * @param Record[] $related
85
	 * @return Record[]
86
	 */
87
	protected function match(array $instances, array $related)
88
	{
89
		$list = $this->adjacencyList($related);
90
		
91
		foreach ($instances as $instance) {
92
			$key = $instance->id();
93
			$value = isset($list[$key]) ? $list[$key] : array();
94
			$instance->relation($this->name)->set($value);
95
		}
96
		
97
		return $instances;
98
	}
99
	
100
	/**
101
	 * Associate the given models.
102
	 * 
103
	 * Loads any currently associated models before attaching and saving
104
	 * the given models.
105
	 * 
106
	 * Returns the number of successfully associated models.
107
	 * 
108
	 * @param Record[]|Record $instances
109
	 * @return int
110
	 */
111 View Code Duplication
	public function associate($instances)
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...
112
	{
113
		$this->verify($instances);
114
		
115
		$this->load();
116
		
117
		$this->attach($instances);
118
		
119
		$ids = static::attributeList($instances, 'id');
120
		
121
		$successful = 0;
122
		
123
		foreach ($this->related as $model) {
124
			$this->persist($model);
125
			
126
			if (!$ids || in_array($model->id(), $ids)) {
127
				$model->set($this->foreignKey, $this->parent->id());
128
				$successful += $model->save();
129
			}
130
		}
131
		
132
		return (int) $successful;
133
	}
134
	
135
	/**
136
	 * Retrieve the related models.
137
	 * 
138
	 * @return Record[]
139
	 */
140
	public function retrieve()
141
	{
142
		return $this->all();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->all(); of type Darya\ORM\Record[]|null adds the type Darya\ORM\Record[] to the return on line 142 which is incompatible with the return type of the parent method Darya\ORM\Relation\Has::retrieve of type Darya\ORM\Record|null.
Loading history...
143
	}
144
	
145
	/**
146
	 * Dissociate all currently associated models.
147
	 * 
148
	 * Returns the number of models successfully dissociated.
149
	 * 
150
	 * TODO: Consider constraints
151
	 * 
152
	 * @return int
153
	 */
154
	public function purge()
155
	{
156
		$this->related = array();
157
		
158
		// return (int) $this->storage()->query($this->target->table())
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
			// ->where($this->foreignKey, 0)
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
160
			// ->update(array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
161
				// $this->foreignKey => $this->parent->get($this->localKey)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
			// ))
163
			// ->cheers()->affected;
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
164
		
165
		return (int) $this->storage()->update($this->target->table(), array(
166
			$this->foreignKey => 0
167
		), array(
168
			$this->foreignKey => $this->parent->get($this->localKey)
169
		));
170
	}
171
}
172