forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigRepository.php
More file actions
227 lines (192 loc) · 5.92 KB
/
Copy pathConfigRepository.php
File metadata and controls
227 lines (192 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* Ushahidi Config Repository
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Repository;
use Ushahidi\Core\Data;
use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\Config as ConfigEntity;
use Ushahidi\Core\Entity\ConfigRepository as ConfigRepositoryContract;
use Ushahidi\Core\Usecase\ReadRepository;
use Ushahidi\Core\Usecase\UpdateRepository;
use Ushahidi\Core\Exception\NotFoundException;
use Ushahidi\App\Multisite\OhanzeeResolver;
use League\Event\ListenerInterface;
use Ushahidi\Core\Traits\Event;
use Ohanzee\DB;
use Ohanzee\Database;
class ConfigRepository implements
ReadRepository,
UpdateRepository,
ConfigRepositoryContract
{
// Use Event trait to trigger events
use Event;
protected $resolver;
public function __construct(OhanzeeResolver $resolver)
{
$this->resolver = $resolver;
}
/**
* Get current connection
*
* @return Ohanzee\Database;
*/
protected function db()
{
return $this->resolver->connection();
}
// ReadRepository
public function getEntity(array $data = null)
{
return new ConfigEntity($data);
}
protected function getDefaults($group)
{
// Just in case we find some other path here
// We absolutely have to validate the group
// since we're now using it as a file name
$this->verifyGroup($group);
// @todo replace with separate entities
$file = __DIR__ . '/Config/' . $group . '.php';
if (file_exists($file)) {
return require $file;
}
return [];
}
// ReadRepository
// ConfigRepository
public function get($group)
{
$this->verifyGroup($group);
$config = [];
try {
$query = DB::select('config.*')
->from('config')
->where('group_name', '=', $group)
->execute($this->db());
if (count($query)) {
$config = $query->as_array('config_key', 'config_value');
$config = array_map(function ($config_value) {
return json_decode($config_value, true);
}, $config);
}
} catch (\Ohanzee\Database\Exception $e) {
// If error was NOT because table doesn't exist
if (!preg_match("/Table '.*' doesn't exist/", $e->getMessage())) {
// Throw the error again
throw $e;
}
// Otherwise, Config table doesn't exist: continue to default values
}
// Merge defaults
$defaults = $this->getDefaults($group);
$config = array_replace_recursive($defaults, $config);
return $this->getEntity(['id' => $group] + $config);
}
// UpdateRepository
public function update(Entity $entity)
{
$intercom_data = [];
$group = $entity->getId();
$this->verifyGroup($group);
// Intercom count datasources
if ($group === 'data-provider') {
$intercom_data['num_data_sources'] = 0;
foreach ($entity->providers as $key => $value) {
$value ? $intercom_data['num_data_sources']++ : null;
}
}
$immutable = $entity->getImmutable();
foreach ($entity->getChanged() as $key => $val) {
// Emit Intercom Update events
if ($key === 'description') {
$intercom_data['has_description'] = true;
}
if ($key === 'image_header') {
$intercom_data['has_logo'] = true;
}
// New User - set their deployment created date
if ($key === 'first_login') {
$intercom_data['deployment_created_date'] = date("Y-m-d H:i:s");
}
if (! in_array($key, $immutable)) {
$this->insertOrUpdate($group, $key, $val);
}
}
if ($intercom_data) {
$this->emit($this->event, $intercom_data);
}
}
private function insertOrUpdate($group, $key, $value)
{
$value = json_encode($value);
DB::query(Database::INSERT, "
INSERT INTO `config`
(`group_name`, `config_key`, `config_value`) VALUES (:group, :key, :value)
ON DUPLICATE KEY UPDATE `config_value` = :value;
")
->parameters([
':group' => $group,
':key' => $key,
':value' => $value
])
->execute($this->db());
}
// ConfigRepository
public function groups()
{
return [
'features',
'site',
'test',
'data-provider',
'map',
'twitter'
];
}
/**
* @param string $group
* @throws InvalidArgumentException when group is invalid
* @return void
*/
protected function verifyGroup($group)
{
if ($group && !in_array($group, $this->groups())) {
throw new NotFoundException('Requested group does not exist: ' . $group);
}
}
/**
* @param array $groups
* @throws InvalidArgumentException when any group is invalid
* @return void
*/
protected function verifyGroups(array $groups)
{
$invalid = array_diff(array_values($groups), $this->groups());
if ($invalid) {
throw new NotFoundException(
'Requested groups do not exist: ' . implode(', ', $invalid)
);
}
}
// ConfigRepository
public function all(array $groups = null)
{
if ($groups) {
$this->verifyGroups($groups);
} else {
$groups = $this->groups();
}
$result = [];
foreach ($groups as $group) {
$result[] = $this->get($group);
}
return $result;
}
}