1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NokitaKaze\Queue; |
4
|
|
|
|
5
|
|
|
class QueueFactory { |
6
|
|
|
/** |
7
|
|
|
* @param GeneralQueueConstructionSettings $settings |
8
|
|
|
* |
9
|
|
|
* @return Queue |
10
|
|
|
* @throws QueueException |
11
|
|
|
*/ |
12
|
33 |
|
static function get_queue($settings) { |
|
|
|
|
13
|
33 |
|
if (!isset($settings->name)) { |
|
|
|
|
14
|
3 |
|
throw new QueueException('Queue name has not been set'); |
15
|
|
|
} |
16
|
30 |
|
if (!isset($settings->sleep_time_while_consuming)) { |
|
|
|
|
17
|
30 |
|
$settings->sleep_time_while_consuming = 0.02; |
|
|
|
|
18
|
10 |
|
} |
19
|
|
|
|
20
|
30 |
|
if (!isset($settings->queues)) { |
|
|
|
|
21
|
30 |
|
if (!isset($settings->scenario) or ($settings->scenario == 0)) { |
|
|
|
|
22
|
18 |
|
$settings->queues = self::get_scenario_read_small_files($settings); |
|
|
|
|
23
|
24 |
|
} elseif ($settings->scenario == 1) { |
|
|
|
|
24
|
18 |
|
$settings->queues = self::get_scenario_write_small_files($settings); |
|
|
|
|
25
|
6 |
|
} else { |
26
|
3 |
|
throw new QueueException("Can not resolve scenario #{$settings->scenario}"); |
|
|
|
|
27
|
|
|
} |
28
|
9 |
|
} |
29
|
|
|
|
30
|
27 |
|
$queue = new Queue($settings); |
31
|
|
|
|
32
|
27 |
|
return $queue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param GeneralQueueConstructionSettings $settings |
37
|
|
|
* |
38
|
|
|
* @return Queue[] |
39
|
|
|
*/ |
40
|
21 |
|
protected static function get_scenario_read_small_files($settings) { |
41
|
|
|
/** |
42
|
|
|
* @var FileDBQueueConstructionSettingsForGeneral $sub_set1 |
43
|
|
|
*/ |
44
|
18 |
|
$sub_set1 = (object) []; |
45
|
18 |
|
$sub_set1->is_general = true; |
|
|
|
|
46
|
18 |
|
$sub_set1->name = $settings->name.'_set1'; |
|
|
|
|
47
|
21 |
|
$sub_set1->folder = $settings->folder; |
|
|
|
|
48
|
18 |
|
$sub_set1->mutex_folder = $settings->folder.'/mutex'; |
|
|
|
|
49
|
18 |
|
$sub_set1->class_name = 'FileDB'; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var SmallFilesQueueConstructionSettingsForGeneral $sub_set2 |
53
|
|
|
*/ |
54
|
21 |
|
$sub_set2 = (object) []; |
55
|
18 |
|
$sub_set2->is_general = false; |
|
|
|
|
56
|
18 |
|
$sub_set2->name = $settings->name.'_set2'; |
|
|
|
|
57
|
18 |
|
$sub_set2->message_folder = $settings->folder.'/messages'; |
|
|
|
|
58
|
18 |
|
$sub_set2->class_name = 'SmallFiles'; |
|
|
|
|
59
|
|
|
|
60
|
18 |
|
return [$sub_set1, $sub_set2]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param GeneralQueueConstructionSettings $settings |
65
|
|
|
* |
66
|
|
|
* @return Queue[] |
67
|
|
|
*/ |
68
|
|
|
protected static function get_scenario_write_small_files($settings) { |
69
|
18 |
|
/** |
70
|
|
|
* @var SmallFilesQueueConstructionSettingsForGeneral $sub_set2 |
71
|
|
|
*/ |
72
|
|
|
$sub_set2 = (object) []; |
73
|
18 |
|
$sub_set2->is_general = true; |
|
|
|
|
74
|
18 |
|
$sub_set2->name = $settings->name.'_set2'; |
|
|
|
|
75
|
18 |
|
$sub_set2->message_folder = $settings->folder.'/messages'; |
|
|
|
|
76
|
18 |
|
$sub_set2->class_name = 'SmallFiles'; |
|
|
|
|
77
|
18 |
|
|
78
|
|
|
return [$sub_set2]; |
79
|
18 |
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
?> |
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.