Completed
Push — master ( 3239e9...e1b4b8 )
by Georgi
27s queued 16s
created

model_after_save()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.024

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 5
cp 0.6
rs 9.9666
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 5.024
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Common association for has-many and many-to-many relationships
4
 *
5
 * @package    Jam
6
 * @category   Associations
7
 * @author     Ivan Kerin
8
 * @copyright  (c) 2012 Despark Ltd.
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
abstract class Kohana_Jam_Association_Collection extends Jam_Association {
12
13
	/**
14
	 * Find the join table based on the two model names pluralized,
15
	 * sorted alphabetically and with an underscore separating them
16
	 *
17
	 * @param  string $model1
18
	 * @param  string $model2
19
	 * @return string
20
	 */
21 36
	static public function guess_through_table($model1, $model2)
22
	{
23
		$through = array(
24 36
			Inflector::plural($model1),
25 36
			Inflector::plural($model2)
26
		);
27
28 36
		sort($through);
29 36
		return implode('_', $through);
30
	}
31
32
	/**
33
	 * Assign default forein_model to singular association name
34
	 * @param  Jam_Meta $meta
35
	 * @param  string   $name
36
	 */
37 93
	public function initialize(Jam_Meta $meta, $name)
38
	{
39 93
		if ( ! $this->foreign_model)
40
		{
41 79
			$this->foreign_model = Inflector::singular($name);
42
		}
43
44 93
		parent::initialize($meta, $name);
45 93
	}
46
47
	/**
48
	 * Load associated models (from database or after deserialization)
49
	 * @param  Jam_Validated $model
50
	 * @param  mixed         $value
51
	 * @return Jam_Array_Association
52
	 */
53 7
	public function load_fields(Jam_Validated $model, $value)
54
	{
55 7
		if ($value instanceof Jam_Array_Association)
56
		{
57
			$collection = $value;
58
		}
59
		else
60
		{
61 7
			$collection = new Jam_Array_Association();
62
			$collection
63 7
				->load_fields($value);
64
		}
65
66 7
		return $this->assign_internals($model, $collection);
67
	}
68
69 754
	public function assign_internals(Jam_Validated $model, Jam_Array_Association $collection)
70
	{
71
		return $collection
72 754
			->model($this->foreign_model)
73 754
			->parent($model)
74 754
			->association($this);
75
	}
76
77 21
	public function get(Jam_Validated $model, $value, $is_changed)
78
	{
79 21
		$array = new Jam_Array_Association();
80
		$array
81 21
			->model($this->foreign_model)
82 21
			->association($this)
83 21
			->parent($model);
84
85 21
		if ($is_changed)
86
		{
87 7
			$array->set($value);
88
		}
89
90 21
		return $array;
91
	}
92
93
	/**
94
	 * Perform checks on all changed items from this collection
95
	 * @param  Jam_Model $model
96
	 */
97 8
	public function model_after_check(Jam_Model $model)
98
	{
99 8
		if ($model->changed($this->name) AND ! $model->{$this->name}->check_changed())
100
		{
101
			$model->errors()->add($this->name, 'association_collection');
102
		}
103 8
	}
104
105
	/**
106
	 * Persist this collection in the database
107
	 * @param  Jam_Model $model
108
	 */
109 8
	public function model_after_save(Jam_Model $model)
110
	{
111 8
		if ($model->changed($this->name) AND $collection = $model->{$this->name} AND $collection->changed())
112
		{
113
			$collection->save_changed();
114
115
			$this->save($model, $collection);
116
		}
117 8
	}
118
119
	/**
120
	 * Use this method to perform special actions when an item is requested from the collection
121
	 * @param  Jam_Model $model
122
	 * @param  Jam_Model $item
123
	 */
124
	public function item_get(Jam_Model $model, Jam_Model $item)
125
	{
126
		// Extend
127
	}
128
129
	/**
130
	 * Use this method to perform special actions when an item is set to the collection
131
	 * @param  Jam_Model $model
132
	 * @param  Jam_Model $item
133
	 */
134
	public function item_set(Jam_Model $model, Jam_Model $item)
135
	{
136
		// Extend
137
	}
138
139
	/**
140
	 * Use this method to perform special actions when an item is removed from the collection
141
	 * @param  Jam_Model $model
142
	 * @param  Jam_Model $item
143
	 */
144
	public function item_unset(Jam_Model $model, Jam_Model $item)
0 ignored issues
show
Unused Code introduced by
The parameter $model 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...
145
	{
146
		// Extend
147
	}
148
149
	/**
150
	 * A database query used to remove items from the model
151
	 * @param  array     $ids
152
	 * @param  Jam_Model $model
153
	 * @return Database_Query
154
	 */
155
	abstract public function remove_items_query(Jam_Model $model, array $ids);
156
157
	/**
158
	 * A database query used to add items to the model
159
	 * @param  array     $ids
160
	 * @param  Jam_Model $model
161
	 * @return Database_Query
162
	 */
163
	abstract public function add_items_query(Jam_Model $model, array $ids);
164
165
	/**
166
	 * Get the colleciton to get the models of the association
167
	 * @param  Jam_Model $model
168
	 * @return Jam_Query_Builder_Collection
169
	 */
170
	abstract public function collection(Jam_Model $model);
171
172
	/**
173
	 * Execute remove_items_query and add_items_query to persist the colleciton to the datbaase
174
	 * @param  Jam_Model                    $model
175
	 * @param  Jam_Array_Association $collection
176
	 */
177 1
	public function save(Jam_Model $model, Jam_Array_Association $collection)
178
	{
179 1
		if ($old_ids = array_values(array_diff($collection->original_ids(), $collection->ids())))
180
		{
181 1
			$query = $this->remove_items_query($model, $old_ids);
182 1
			if ($query)
183
			{
184 1
				$query->execute(Jam::meta($this->model)->db());
185
			}
186
		}
187
188 1
		if ($new_ids = array_values(array_diff($collection->ids(), $collection->original_ids())))
189
		{
190 1
			$query = $this->add_items_query($model, $new_ids);
191 1
			if ($query)
192
			{
193 1
				$query->execute(Jam::meta($this->model)->db());
194
			}
195
		}
196 1
	}
197
198
	/**
199
	 * Use the remove query to remove all items from the collection
200
	 * @param  Jam_Validated                $model
201
	 * @param  Jam_Array_Association $collection
202
	 */
203 1
	public function clear(Jam_Validated $model, Jam_Array_Association $collection)
204
	{
205 1
		if ($ids = array_filter($collection->ids()))
206
		{
207 1
			$query = $this->remove_items_query($model, $ids);
0 ignored issues
show
Compatibility introduced by
$model of type object<Jam_Validated> is not a sub-type of object<Jam_Model>. It seems like you assume a child class of the class Jam_Validated to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
208 1
			if ($query)
209
			{
210 1
				$query->execute(Jam::meta($this->model)->db());
211
			}
212
		}
213 1
	}
214
215
}
216