Hooks   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 147
c 0
b 0
f 0
wmc 15
lcom 0
cbo 4
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B synthesize_config() 0 26 5
B criteria_from() 0 26 4
A criterion_list_from() 0 16 2
A fetch_records() 0 14 2
A fetch_record() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Binding\Facets;
13
14
use ICanBoogie\ActiveRecord;
15
use ICanBoogie\ActiveRecord\Model;
16
use ICanBoogie\Binding\PrototypedBindings;
17
use ICanBoogie\Facets\CriterionList;
18
use ICanBoogie\Facets\Fetcher;
19
use ICanBoogie\Facets\Fetcher\BasicFetcher;
20
use ICanBoogie\Facets\RecordCollection;
21
22
class Hooks
23
{
24
	/**
25
	 * Synthesizes the `activerecord_facets` config from `activerecord` fragments.
26
	 *
27
	 * @param array $fragments
28
	 *
29
	 * @return array
30
	 */
31
	static public function synthesize_config(array $fragments)
32
	{
33
		$facets = [];
34
35
		foreach ($fragments as $fragment)
36
		{
37
			if (empty($fragment['facets']))
38
			{
39
				continue;
40
			}
41
42
			foreach ($fragment['facets'] as $model_id => $criteria)
43
			{
44
				if (empty($facets[$model_id]))
45
				{
46
					$facets[$model_id] = $criteria;
47
48
					continue;
49
				}
50
51
				$facets[$model_id] = array_merge($facets[$model_id], $criteria);
52
			}
53
		}
54
55
		return $facets;
56
	}
57
58
	/**
59
	 * Returns the criteria associated with the specified model.
60
	 *
61
	 * The criteria include the criteria of the parent models.
62
	 *
63
	 * @param Model|PrototypedBindings $model
64
	 *
65
	 * @return array
66
	 */
67
	static public function criteria_from(Model $model)
68
	{
69
		$criteria_list = [];
70
		$facets = $model->app->configs['activerecord_facets'];
71
72
		$m = $model;
73
74
		while ($m)
75
		{
76
			$id = $m->id;
77
78
			if (!empty($facets[$id]))
79
			{
80
				$criteria_list[] = $facets[$id];
81
			}
82
83
			$m = $m->parent_model;
84
		}
85
86
		if (!$criteria_list)
87
		{
88
			return [];
89
		}
90
91
		return call_user_func_array('array_merge', array_reverse($criteria_list));
92
	}
93
94
	/**
95
	 * Returns the {@link CriterionList} instance associated with the specified model.
96
	 *
97
	 * @param Model|ModelBindings $model
98
	 *
99
	 * @return CriterionList
100
	 */
101
	static public function criterion_list_from(Model $model)
102
	{
103
		static $instances = [];
104
105
		$model_id = $model->id;
106
107
		if (isset($instances[$model_id]))
108
		{
109
			return $instances[$model_id];
110
		}
111
112
		$criteria = $model->criteria;
113
		$instances[$model_id] = $criterion_list = new CriterionList($criteria);
114
115
		return $criterion_list;
116
	}
117
118
	/**
119
	 * Fetches the records matching the specified conditions.
120
	 *
121
	 * A {@link BasicFetcher} instance is used to fetch the records.
122
	 *
123
	 * @param Model $model
124
	 * @param array $conditions
125
	 *
126
	 * @return RecordCollection|null
127
	 */
128
	static public function fetch_records(Model $model, array $conditions)
129
	{
130
		$fetcher = new BasicFetcher($model);
131
		$records = $fetcher($conditions);
132
133
		if (!$records)
134
		{
135
			return null;
136
		}
137
138
		new RecordCollection\AlterEvent($records);
139
140
		return $records;
141
	}
142
143
	/**
144
	 * Fetches a record matching the specified conditions.
145
	 *
146
	 * The model's {@link fetch_records} prototype method is used to retrieve the record.
147
	 *
148
	 * @param Model|ModelBindings $model
149
	 * @param array $conditions
150
	 * @param Fetcher $fetcher If the parameter `fetcher` is present, the {@link Fetcher}
151
	 * instance used to fetch the record is stored inside.
152
	 *
153
	 * @return ActiveRecord|null
154
	 */
155
	static public function fetch_record(Model $model, array $conditions, &$fetcher = null)
156
	{
157
		$records = $model->fetch_records($conditions + [ 'limit' => 1 ]);
158
159
		if (!$records)
160
		{
161
			return null;
162
		}
163
164
		$fetcher = $records->fetcher;
165
166
		return $records->one;
167
	}
168
}
169