Complex classes like FileDBQueueTransport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileDBQueueTransport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class FileDBQueueTransport extends AbstractQueueTransport { |
||
8 | const FILE_DB_INDEX_VERSION = 1; |
||
9 | const FILE_DB_CHUNK_VERSION = 1; |
||
10 | |||
11 | protected $_folder; |
||
12 | protected $_mutex_folder; |
||
13 | protected $_prefix; |
||
14 | protected $_name; |
||
15 | protected $_db_file_count = Queue::DefaultDBFileCount; |
||
16 | /** |
||
17 | * @var FileDBQueueConstructionSettings|object |
||
18 | */ |
||
19 | protected $_construction_settings; |
||
20 | |||
21 | /** |
||
22 | * @var FileMutex|null |
||
23 | */ |
||
24 | protected $_producer_mutex = null; |
||
25 | |||
26 | /** |
||
27 | * @var FileMutex|null |
||
28 | */ |
||
29 | protected $_index_mutex = null; |
||
30 | |||
31 | /** |
||
32 | * @var integer[][] Данные с индексом |
||
33 | */ |
||
34 | protected $_index_data = []; |
||
35 | |||
36 | /** |
||
37 | * @var FileDBIndexFile |
||
38 | */ |
||
39 | protected $_index_data_full = null; |
||
40 | |||
41 | /** |
||
42 | * @var boolean Эксклюзивный режим |
||
43 | */ |
||
44 | protected $_exclusive_mode = false; |
||
45 | |||
46 | /** |
||
47 | * @param FileDBQueueConstructionSettings|object $settings |
||
48 | * |
||
49 | * @throws QueueException |
||
50 | */ |
||
51 | 345 | function __construct($settings) { |
|
1 ignored issue
–
show
|
|||
52 | 345 | if (!isset($settings->storage_type)) { |
|
53 | 342 | $settings->storage_type = Queue::StorageTemporary; |
|
54 | 85 | } |
|
55 | 345 | if (!isset($settings->name)) { |
|
56 | 3 | throw new QueueException('Settings don\'t have field name', 11); |
|
57 | } |
||
58 | 342 | $this->_name = $settings->name; |
|
59 | 342 | $this->_construction_settings = $settings; |
|
60 | |||
61 | 342 | $this->set_folder_settings($settings); |
|
62 | 339 | if (isset($settings->prefix)) { |
|
63 | 3 | $this->_prefix = $settings->prefix; |
|
64 | 1 | } else { |
|
65 | 336 | $this->_prefix = ''; |
|
66 | } |
||
67 | 339 | if (isset($settings->mutex_folder)) { |
|
68 | 21 | $this->_mutex_folder = $settings->mutex_folder; |
|
69 | 7 | } else { |
|
70 | 321 | $this->_mutex_folder = $this->_folder.'/mutex'; |
|
71 | } |
||
72 | 339 | if (isset($settings->db_file_count)) { |
|
73 | 3 | $this->_db_file_count = $settings->db_file_count; |
|
74 | 1 | } |
|
75 | 339 | $this->_index_data = array_fill(0, $this->_db_file_count, []); |
|
76 | 339 | } |
|
77 | |||
78 | 2457 | function __destruct() { |
|
1 ignored issue
–
show
|
|||
79 | 2457 | if (!is_null($this->_producer_mutex)) { |
|
80 | 2433 | $this->_producer_mutex->release_lock(); |
|
81 | 782 | } |
|
82 | 2457 | if (!is_null($this->_index_mutex)) { |
|
83 | 2433 | $this->_index_mutex->release_lock(); |
|
84 | 782 | } |
|
85 | 2457 | } |
|
86 | |||
87 | 2148 | function __clone() { |
|
100 | |||
101 | /** |
||
102 | * @param FileDBQueueConstructionSettings|object $settings |
||
103 | * |
||
104 | * @throws QueueException |
||
105 | */ |
||
106 | 342 | protected function set_folder_settings($settings) { |
|
124 | |||
125 | /** |
||
126 | * Блокируем index mutex |
||
127 | * |
||
128 | * @param double|integer $time |
||
129 | * |
||
130 | * @return boolean |
||
131 | */ |
||
132 | 2430 | protected function index_mutex_lock($time = -1) { |
|
139 | |||
140 | /** |
||
141 | * Блокируем index mutex |
||
142 | */ |
||
143 | 2430 | protected function index_mutex_release_lock() { |
|
148 | |||
149 | /** |
||
150 | * Устанавливаем или снимаем монопольный режим |
||
151 | * |
||
152 | * @param boolean $mode |
||
153 | */ |
||
154 | 118 | function set_exclusive_mode($mode) { |
|
163 | |||
164 | /** |
||
165 | * @return iMessage[]|object[] |
||
166 | */ |
||
167 | 2430 | protected function get_used_keys_and_real_need_for_save() { |
|
189 | |||
190 | /** |
||
191 | * Сохраняем все сообщения, подготовленные для сохранения, в файл сообщений |
||
192 | * |
||
193 | * @throws QueueException |
||
194 | */ |
||
195 | 2433 | function save() { |
|
240 | |||
241 | /** |
||
242 | * Берём данные для конкретного внутренного треда в очереди сообщений |
||
243 | * |
||
244 | * @param integer $thread_id |
||
245 | * |
||
246 | * @return iMessage[]|object[] |
||
247 | * @throws QueueException |
||
248 | */ |
||
249 | 2430 | protected function get_data_for_thread($thread_id) { |
|
275 | |||
276 | /** |
||
277 | * Внутренний id треда |
||
278 | * |
||
279 | * @return integer |
||
280 | */ |
||
281 | 1565 | protected static function get_current_producer_thread_id() { |
|
284 | |||
285 | /** |
||
286 | * Берём название файла с данными для конкретного внутренного треда в очереди сообщений |
||
287 | * |
||
288 | * @param integer $thread_id |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | 2430 | protected function get_producer_filename_for_thread($thread_id) { |
|
295 | |||
296 | /** |
||
297 | * Инициализируем мьютекс для текущего треда |
||
298 | */ |
||
299 | 2433 | protected function init_producer_mutex() { |
|
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | 2433 | function get_mutex_folder() { |
|
312 | |||
313 | /** |
||
314 | * Мьютекс для конкретного треда |
||
315 | * |
||
316 | * @param integer $thread_id |
||
317 | * |
||
318 | * @return FileMutex |
||
319 | */ |
||
320 | 2436 | protected function get_mutex_for_thread($thread_id) { |
|
332 | |||
333 | /** |
||
334 | * Создание мьютекса для файла с индексом на всю текущую очередь сообщений |
||
335 | * |
||
336 | * @return FileMutex |
||
337 | */ |
||
338 | 2436 | protected function get_index_mutex() { |
|
350 | |||
351 | /** |
||
352 | * Сохраняем данные в файл внутреннего треда (без индекса) |
||
353 | * |
||
354 | * @param integer $thread_id |
||
355 | * @param iMessage[]|object[] $data_in |
||
356 | * |
||
357 | * @throws QueueException |
||
358 | */ |
||
359 | 2433 | protected function write_full_data_to_file($thread_id, $data_in) { |
|
394 | |||
395 | /** |
||
396 | * Получаем номера DB, в которых лежат сообщения, которые нам надо удалить |
||
397 | * |
||
398 | * @param iMessage[]|object[] $messages |
||
399 | * |
||
400 | * @return string[][]|integer[][] |
||
401 | */ |
||
402 | 2202 | protected function get_keys_for_delete_group_by_thread($messages) { |
|
424 | |||
425 | /** |
||
426 | * Удалить сообщения и сразу же записать это в БД |
||
427 | * |
||
428 | * @param iMessage[]|object[] $messages |
||
429 | * |
||
430 | * @return string[]|integer[] |
||
431 | */ |
||
432 | 2298 | function delete_messages(array $messages) { |
|
472 | |||
473 | /** |
||
474 | * Обновляем сообщение и сразу же сохраняем всё |
||
475 | * |
||
476 | * Эта функция не рейзит ошибку, если сообщение не найдено |
||
477 | * |
||
478 | * @param iMessage|object $message |
||
479 | * @param string|null $key форсированно задаём ключ сообщения |
||
480 | * |
||
481 | * @return boolean |
||
482 | */ |
||
483 | 109 | function update_message($message, $key = null) { |
|
514 | |||
515 | /** |
||
516 | * @param double|integer $wait_time |
||
517 | * |
||
518 | * @return iMessage|object|null |
||
519 | */ |
||
520 | 2433 | function consume_next_message($wait_time = -1) { |
|
566 | |||
567 | /** |
||
568 | * @return string |
||
569 | */ |
||
570 | 2439 | function get_queue_name() { |
|
573 | |||
574 | /** |
||
575 | * Поддерживает ли очередь сортировку event'ов при вставке |
||
576 | * |
||
577 | * @return boolean |
||
578 | */ |
||
579 | 6 | static function is_support_sorted_events() { |
|
582 | |||
583 | /** |
||
584 | * Индексы |
||
585 | */ |
||
586 | |||
587 | /** |
||
588 | * Название файла для содержания даты |
||
589 | * |
||
590 | * @return string |
||
591 | */ |
||
592 | 2433 | protected function get_index_filename() { |
|
595 | |||
596 | /** |
||
597 | * @throws QueueException |
||
598 | */ |
||
599 | 2430 | protected function index_data_load() { |
|
625 | |||
626 | /** |
||
627 | * Сохраняем данные в индекс |
||
628 | * |
||
629 | * @throws QueueException |
||
630 | */ |
||
631 | 2433 | protected function index_data_save() { |
|
668 | |||
669 | /** |
||
670 | * @param FileDBQueueTransport $queue |
||
671 | * |
||
672 | * @return boolean |
||
673 | */ |
||
674 | 750 | function is_equal_to($queue) { |
|
686 | } |
||
687 | |||
688 | ?> |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.