Filter
3 days ago
Rule
3 days ago
Sort
3 days ago
Storage
3 days ago
Base.php
3 days ago
Database.php
3 days ago
Filter.php
3 days ago
Rule.php
3 days ago
Rules.php
3 days ago
Sort.php
3 days ago
Storage.php
3 days ago
Types.php
3 days ago
Database.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\ListScreenRepository; |
| 6 | |
| 7 | use AC\ColumnFactories\Aggregate; |
| 8 | use AC\ColumnIterator; |
| 9 | use AC\ColumnIterator\ProxyColumnIterator; |
| 10 | use AC\ColumnRepository\EncodedData; |
| 11 | use AC\Exception\FailedToSaveListScreen; |
| 12 | use AC\ListScreen; |
| 13 | use AC\ListScreenCollection; |
| 14 | use AC\ListScreenRepositoryWritable; |
| 15 | use AC\Setting\ConfigCollection; |
| 16 | use AC\Storage\Encoder\BaseEncoder; |
| 17 | use AC\Storage\EncoderFactory; |
| 18 | use AC\Storage\Repository\OriginalColumnsRepository; |
| 19 | use AC\TableScreen; |
| 20 | use AC\TableScreenFactory; |
| 21 | use AC\Type\ListScreenId; |
| 22 | use AC\Type\ListScreenStatus; |
| 23 | use AC\Type\TableId; |
| 24 | use DateTime; |
| 25 | use LogicException; |
| 26 | |
| 27 | class Database extends Base implements ListScreenRepositoryWritable |
| 28 | { |
| 29 | private const TABLE = 'admin_columns'; |
| 30 | |
| 31 | private TableScreenFactory $table_screen_factory; |
| 32 | |
| 33 | private EncoderFactory $encoder_factory; |
| 34 | |
| 35 | private Aggregate $column_factory; |
| 36 | |
| 37 | private OriginalColumnsRepository $original_columns_repository; |
| 38 | |
| 39 | public function __construct( |
| 40 | TableScreenFactory $table_screen_factory, |
| 41 | EncoderFactory $encoder_factory, |
| 42 | Aggregate $column_factory, |
| 43 | OriginalColumnsRepository $original_columns_repository |
| 44 | ) { |
| 45 | $this->table_screen_factory = $table_screen_factory; |
| 46 | $this->encoder_factory = $encoder_factory; |
| 47 | $this->column_factory = $column_factory; |
| 48 | $this->original_columns_repository = $original_columns_repository; |
| 49 | } |
| 50 | |
| 51 | public function find(ListScreenId $id): ?ListScreen |
| 52 | { |
| 53 | global $wpdb; |
| 54 | |
| 55 | $sql = $wpdb->prepare( |
| 56 | ' |
| 57 | SELECT * |
| 58 | FROM ' . $wpdb->prefix . self::TABLE . ' |
| 59 | WHERE list_id = %s |
| 60 | ', |
| 61 | (string)$id |
| 62 | ); |
| 63 | |
| 64 | $row = $wpdb->get_row($sql); |
| 65 | |
| 66 | if (! $row) { |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | return $this->create_list_screen($row); |
| 71 | } |
| 72 | |
| 73 | public function find_all(?Sort $sort = null): ListScreenCollection |
| 74 | { |
| 75 | global $wpdb; |
| 76 | |
| 77 | $sql = ' |
| 78 | SELECT * |
| 79 | FROM ' . $wpdb->prefix . self::TABLE . ' |
| 80 | '; |
| 81 | |
| 82 | return $this->sort( |
| 83 | $this->create_list_screens($wpdb->get_results($sql)), |
| 84 | $sort |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | public function find_all_by_table_id( |
| 89 | TableId $table_id, |
| 90 | ?Sort $sort = null, |
| 91 | ?ListScreenStatus $status = null |
| 92 | ): ListScreenCollection { |
| 93 | global $wpdb; |
| 94 | |
| 95 | $sql = $wpdb->prepare( |
| 96 | ' |
| 97 | SELECT * |
| 98 | FROM ' . $wpdb->prefix . self::TABLE . ' |
| 99 | WHERE list_key = %s |
| 100 | ', |
| 101 | (string)$table_id, |
| 102 | ); |
| 103 | |
| 104 | if ($status) { |
| 105 | $is_empty = '' === (string)$status; |
| 106 | |
| 107 | $sql .= $is_empty |
| 108 | ? " AND ( status = '' OR STATUS IS NULL )" |
| 109 | : $wpdb->prepare(' AND status = %s', (string)$status); |
| 110 | } |
| 111 | |
| 112 | return $this->sort( |
| 113 | $this->create_list_screens($wpdb->get_results($sql)), |
| 114 | $sort |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @throws FailedToSaveListScreen |
| 120 | */ |
| 121 | public function save(ListScreen $list_screen): void |
| 122 | { |
| 123 | global $wpdb; |
| 124 | |
| 125 | $encoder = $this->encoder_factory->create(); |
| 126 | |
| 127 | if (! $encoder instanceof BaseEncoder) { |
| 128 | throw new LogicException('Encoder does not support setting a list screen.'); |
| 129 | } |
| 130 | |
| 131 | $list_screen_dto = $encoder |
| 132 | ->set_list_screen($list_screen) |
| 133 | ->encode()['list_screen']; |
| 134 | |
| 135 | $settings = $this->save_preferences($list_screen_dto); |
| 136 | $date = DateTime::createFromFormat('U', (string)$list_screen_dto['updated']); |
| 137 | |
| 138 | if (! $date instanceof DateTime) { |
| 139 | throw new FailedToSaveListScreen('Failed to save list screen: invalid update timestamp.'); |
| 140 | } |
| 141 | |
| 142 | $args = [ |
| 143 | 'list_id' => $list_screen_dto['id'], |
| 144 | 'list_key' => $list_screen_dto['type'], |
| 145 | 'title' => $list_screen_dto['title'], |
| 146 | 'columns' => $list_screen_dto['columns'] ? serialize($list_screen_dto['columns']) : null, |
| 147 | 'settings' => $settings ? serialize($settings) : null, |
| 148 | 'date_modified' => $date->format('Y-m-d H:i:s'), |
| 149 | 'status' => (string)$list_screen_dto['status'], |
| 150 | ]; |
| 151 | |
| 152 | $table = $wpdb->prefix . self::TABLE; |
| 153 | $exists = $this->exists($list_screen->get_id()); |
| 154 | |
| 155 | if ($exists) { |
| 156 | $wpdb->update( |
| 157 | $table, |
| 158 | $args, |
| 159 | [ |
| 160 | 'list_id' => (string)$list_screen->get_id(), |
| 161 | ], |
| 162 | array_fill(0, 7, '%s') |
| 163 | ); |
| 164 | } else { |
| 165 | $args['date_created'] = $args['date_modified']; |
| 166 | |
| 167 | $wpdb->insert( |
| 168 | $table, |
| 169 | $args, |
| 170 | array_fill(0, 8, '%s') |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | if ($wpdb->last_error) { |
| 175 | throw new FailedToSaveListScreen('Failed to save list screen: ' . $wpdb->last_error); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public function delete(ListScreen $list_screen): void |
| 180 | { |
| 181 | global $wpdb; |
| 182 | |
| 183 | $wpdb->delete( |
| 184 | $wpdb->prefix . self::TABLE, |
| 185 | [ |
| 186 | 'list_id' => (string)$list_screen->get_id(), |
| 187 | ], |
| 188 | [ |
| 189 | '%s', |
| 190 | ] |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Template method to add and remove preferences before save |
| 196 | */ |
| 197 | protected function save_preferences(array $list_screen_dto): array |
| 198 | { |
| 199 | return $list_screen_dto['settings']; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Template method to add and remove preferences before retrieval |
| 204 | * |
| 205 | * @param object{settings?: string|null} $data |
| 206 | */ |
| 207 | protected function get_preferences(object $data): array |
| 208 | { |
| 209 | $settings = $data->settings |
| 210 | ? unserialize($data->settings, ['allowed_classes' => false]) |
| 211 | : []; |
| 212 | |
| 213 | return is_array($settings) ? $settings : []; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param object{list_key: string, list_id: string, title: string, status: string, date_modified: string, columns?: string|null, settings?: string|null} $data |
| 218 | */ |
| 219 | protected function create_list_screen(object $data): ?ListScreen |
| 220 | { |
| 221 | $table_id = new TableId($data->list_key); |
| 222 | |
| 223 | if (! $this->table_screen_factory->can_create($table_id)) { |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | $table_screen = $this->table_screen_factory->create($table_id); |
| 228 | |
| 229 | return new ListScreen( |
| 230 | new ListScreenId($data->list_id), |
| 231 | $data->title, |
| 232 | $table_screen, |
| 233 | $this->create_column_iterator($table_screen, $data), |
| 234 | $this->get_preferences($data), |
| 235 | new ListScreenStatus($data->status), |
| 236 | new DateTime($data->date_modified) |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @param object{columns?: string|null} $data |
| 242 | */ |
| 243 | private function create_column_iterator(TableScreen $table_screen, object $data): ColumnIterator |
| 244 | { |
| 245 | return new ProxyColumnIterator( |
| 246 | new EncodedData( |
| 247 | $this->column_factory->create($table_screen), |
| 248 | $this->create_configs($data), |
| 249 | $this->original_columns_repository, |
| 250 | $table_screen |
| 251 | ) |
| 252 | ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @param object{columns?: string|null} $data |
| 257 | */ |
| 258 | private function create_configs(object $data): ConfigCollection |
| 259 | { |
| 260 | $columns = $data->columns |
| 261 | ? unserialize($data->columns, ['allowed_classes' => false]) |
| 262 | : []; |
| 263 | |
| 264 | return ConfigCollection::create_from_array(is_array($columns) ? $columns : []); |
| 265 | } |
| 266 | |
| 267 | private function create_list_screens(array $rows): ListScreenCollection |
| 268 | { |
| 269 | $list_screens = array_filter( |
| 270 | array_map([$this, 'create_list_screen'], $rows) |
| 271 | ); |
| 272 | |
| 273 | return new ListScreenCollection($list_screens); |
| 274 | } |
| 275 | |
| 276 | } |
| 277 |