@@ -32,6 +32,8 @@ final class ParallelizationInput
32
32
public const PROCESSES_OPTION = 'processes ' ;
33
33
public const MAIN_PROCESS_OPTION = 'main-process ' ;
34
34
public const CHILD_OPTION = 'child ' ;
35
+ public const BATCH_SIZE = 'batch-size ' ;
36
+ public const SEGMENT_SIZE = 'segment-size ' ;
35
37
36
38
public const OPTIONS = [
37
39
self ::PROCESSES_OPTION ,
@@ -52,18 +54,33 @@ final class ParallelizationInput
52
54
private $ findNumberOfProcesses ;
53
55
54
56
private ?string $ item ;
57
+
55
58
private bool $ childProcess ;
56
59
60
+ /**
61
+ * @var positive-int|null
62
+ */
63
+ private ?int $ batchSize ;
64
+
65
+ /**
66
+ * @var positive-int|null
67
+ */
68
+ private ?int $ segmentSize ;
69
+
57
70
/**
58
71
* @internal Use the static factory methods instead.
59
72
*
60
73
* @param positive-int|callable():positive-int $numberOfOrFindNumberOfProcesses
74
+ * @param positive-int|null $batchSize
75
+ * @param positive-int|null $segmentSize
61
76
*/
62
77
public function __construct (
63
78
bool $ mainProcess ,
64
79
$ numberOfOrFindNumberOfProcesses ,
65
80
?string $ item ,
66
- bool $ childProcess
81
+ bool $ childProcess ,
82
+ ?int $ batchSize ,
83
+ ?int $ segmentSize
67
84
) {
68
85
$ this ->mainProcess = $ mainProcess ;
69
86
$ this ->item = $ item ;
@@ -74,6 +91,9 @@ public function __construct(
74
91
} else {
75
92
$ this ->findNumberOfProcesses = $ numberOfOrFindNumberOfProcesses ;
76
93
}
94
+
95
+ $ this ->batchSize = $ batchSize ;
96
+ $ this ->segmentSize = $ segmentSize ;
77
97
}
78
98
79
99
public static function fromInput (InputInterface $ input ): self
@@ -87,6 +107,10 @@ public static function fromInput(InputInterface $input): self
87
107
$ mainProcess = $ input ->getOption (self ::MAIN_PROCESS_OPTION );
88
108
/** @var bool $isChild */
89
109
$ isChild = $ input ->getOption (self ::CHILD_OPTION );
110
+ /** @var string|int|null $batchSize */
111
+ $ batchSize = $ input ->getOption (self ::BATCH_SIZE );
112
+ /** @var string|int|null $segmentSize */
113
+ $ segmentSize = $ input ->getOption (self ::SEGMENT_SIZE );
90
114
91
115
if ($ hasItem ) {
92
116
$ item = self ::validateItem ($ item );
@@ -114,11 +138,24 @@ public static function fromInput(InputInterface $input): self
114
138
);
115
139
}
116
140
141
+ $ batchSize = self ::coerceAndValidatePositiveInt (
142
+ $ batchSize ,
143
+ 'batch size ' ,
144
+ true ,
145
+ );
146
+ $ segmentSize = self ::coerceAndValidatePositiveInt (
147
+ $ segmentSize ,
148
+ 'segment size ' ,
149
+ true ,
150
+ );
151
+
117
152
return new self (
118
153
$ mainProcess ,
119
154
$ validatedNumberOfProcesses ,
120
155
$ hasItem ? $ item : null ,
121
156
$ isChild ,
157
+ $ batchSize ,
158
+ $ segmentSize ,
122
159
);
123
160
}
124
161
@@ -152,6 +189,18 @@ public static function configureCommand(Command $command): void
152
189
null ,
153
190
InputOption::VALUE_NONE ,
154
191
'Set on child processes. ' ,
192
+ )
193
+ ->addOption (
194
+ self ::BATCH_SIZE ,
195
+ null ,
196
+ InputOption::VALUE_REQUIRED ,
197
+ 'Set the batch size. ' ,
198
+ )
199
+ ->addOption (
200
+ self ::SEGMENT_SIZE ,
201
+ null ,
202
+ InputOption::VALUE_REQUIRED ,
203
+ 'Set the segment size. ' ,
155
204
);
156
205
}
157
206
@@ -182,6 +231,22 @@ public function isChildProcess(): bool
182
231
return $ this ->childProcess ;
183
232
}
184
233
234
+ /**
235
+ * @return positive-int|null
236
+ */
237
+ public function getBatchSize (): ?int
238
+ {
239
+ return $ this ->batchSize ;
240
+ }
241
+
242
+ /**
243
+ * @return positive-int|null
244
+ */
245
+ public function getSegmentSize (): ?int
246
+ {
247
+ return $ this ->segmentSize ;
248
+ }
249
+
185
250
/**
186
251
* @param mixed $item
187
252
*/
@@ -210,36 +275,39 @@ private static function validateItem($item): string
210
275
*/
211
276
private static function coerceNumberOfProcesses (string $ numberOfProcesses ): int
212
277
{
213
- Assert:: numeric (
278
+ return self :: coerceAndValidatePositiveInt (
214
279
$ numberOfProcesses ,
215
- sprintf (
216
- 'Expected the number of process defined to be a valid numeric value. Got "%s". ' ,
217
- $ numberOfProcesses ,
218
- ),
280
+ 'maximum number of parallel processes ' ,
281
+ false ,
219
282
);
283
+ }
220
284
221
- $ castedNumberOfProcesses = (int ) $ numberOfProcesses ;
285
+ /**
286
+ * @param string|int|null $value
287
+ *
288
+ * @return ($nullable is true ? positive-int|null : positive-int)
289
+ */
290
+ private static function coerceAndValidatePositiveInt (
291
+ $ value ,
292
+ string $ name ,
293
+ bool $ nullable
294
+ ): ?int {
295
+ if ($ nullable && null === $ value ) {
296
+ return null ;
297
+ }
222
298
223
- Assert::same (
224
- // We cast it again in string to make sure since it is more convenient to pass an
225
- // int in the tests or when calling the command directly without passing by the CLI
226
- (string ) $ numberOfProcesses ,
227
- (string ) $ castedNumberOfProcesses ,
228
- sprintf (
229
- 'Expected the number of process defined to be an integer. Got "%s". ' ,
230
- $ numberOfProcesses ,
231
- ),
299
+ $ message = sprintf (
300
+ 'Expected the %s to be an integer greater than or equal to 1. Got "%s". ' ,
301
+ $ name ,
302
+ $ value ,
232
303
);
233
304
234
- Assert::greaterThan (
235
- $ castedNumberOfProcesses ,
236
- 0 ,
237
- sprintf (
238
- 'Expected the number of processes to be 1 or greater. Got "%s". ' ,
239
- $ castedNumberOfProcesses ,
240
- ),
241
- );
305
+ Assert::integerish ($ value , $ message );
306
+
307
+ $ value = (int ) $ value ;
308
+
309
+ Assert::positiveInteger ($ value , $ message );
242
310
243
- return $ castedNumberOfProcesses ;
311
+ return $ value ;
244
312
}
245
313
}
0 commit comments