-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathDatabase.php
192 lines (154 loc) · 4.91 KB
/
Database.php
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
<?php
declare(strict_types=1);
namespace AC\ListScreenRepository;
use AC\Exception\MissingListScreenIdException;
use AC\ListScreen;
use AC\ListScreenCollection;
use AC\ListScreenFactory;
use AC\ListScreenRepositoryWritable;
use AC\Type\ListScreenId;
use LogicException;
class Database implements ListScreenRepositoryWritable
{
use ListScreenRepositoryTrait;
private const TABLE = 'admin_columns';
private $list_screen_factory;
public function __construct(ListScreenFactory $list_screen_factory)
{
$this->list_screen_factory = $list_screen_factory;
}
protected function find_from_source(ListScreenId $id): ?ListScreen
{
global $wpdb;
$sql = $wpdb->prepare(
'
SELECT *
FROM ' . $wpdb->prefix . self::TABLE . '
WHERE list_id = %s
',
(string)$id
);
$row = $wpdb->get_row($sql);
if ( ! $row) {
return null;
}
return $this->create_list_screen($row);
}
protected function find_all_from_source(): ListScreenCollection
{
global $wpdb;
$sql = '
SELECT *
FROM ' . $wpdb->prefix . self::TABLE . '
';
return $this->create_list_screens(
$wpdb->get_results($sql)
);
}
protected function find_all_by_key_from_source(string $key): ListScreenCollection
{
global $wpdb;
$sql = $wpdb->prepare(
'
SELECT *
FROM ' . $wpdb->prefix . self::TABLE . '
WHERE list_key = %s
',
$key
);
return $this->create_list_screens(
$wpdb->get_results($sql)
);
}
public function save(ListScreen $list_screen): void
{
global $wpdb;
if ( ! $list_screen->has_id()) {
throw MissingListScreenIdException::from_saving_list_screen();
}
$list_screen->set_preferences($this->save_preferences($list_screen));
$args = [
'list_id' => (string)$list_screen->get_id(),
'list_key' => $list_screen->get_key(),
'title' => $list_screen->get_title(),
'columns' => $list_screen->get_settings() ? serialize($list_screen->get_settings()) : null,
'settings' => $list_screen->get_preferences() ? serialize($list_screen->get_preferences()) : null,
'date_modified' => $list_screen->get_updated()->format('Y-m-d H:i:s'),
];
$table = $wpdb->prefix . self::TABLE;
$exists = $this->exists($list_screen->get_id());
if ($exists) {
$wpdb->update(
$table,
$args,
[
'list_id' => (string)$list_screen->get_id(),
],
array_fill(0, 6, '%s')
);
} else {
$args['date_created'] = $args['date_modified'];
$wpdb->insert(
$table,
$args,
array_fill(0, 7, '%s')
);
}
}
public function delete(ListScreen $list_screen): void
{
global $wpdb;
if ( ! $list_screen->has_id()) {
throw new LogicException('Cannot delete a ListScreen without an identity.');
}
$wpdb->delete(
$wpdb->prefix . self::TABLE,
[
'list_id' => (string)$list_screen->get_id(),
],
[
'%s',
]
);
}
/**
* Template method to add and remove preferences before save
*/
protected function save_preferences(ListScreen $list_screen): array
{
return $list_screen->get_preferences();
}
/**
* Template method to add and remove preferences before retrieval
*/
protected function get_preferences(ListScreen $list_screen): array
{
return $list_screen->get_preferences();
}
private function create_list_screen(object $data): ?ListScreen
{
if ( ! $this->list_screen_factory->can_create($data->list_key)) {
return null;
}
$list_screen = $this->list_screen_factory->create(
$data->list_key,
[
'title' => $data->title,
'list_id' => $data->list_id,
'date' => $data->date_modified,
'preferences' => $data->settings ? unserialize($data->settings, ['allowed_classes' => false]) : [],
'columns' => $data->columns ? unserialize($data->columns, ['allowed_classes' => false]) : [],
'group' => null,
]
);
$list_screen->set_preferences($this->get_preferences($list_screen));
return $list_screen;
}
private function create_list_screens(array $rows): ListScreenCollection
{
$list_screens = array_filter(
array_map([$this, 'create_list_screen'], $rows)
);
return new ListScreenCollection($list_screens);
}
}