-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_processing.php
50 lines (38 loc) · 1.22 KB
/
order_processing.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
<?php
require_once 'vendor/autoload.php';
use Orders\Order\Factory\OrderFactory;
use Orders\Order\Processor\OrderProcessor;
use Orders\Error\Factory\ErrorFactory;
use Orders\Order\Processor\Result\Factory\OrderProcessorResultFactory;
use Orders\Order\Validator\OrderValidator;
use Orders\Logger\FileLogger;
use Orders\Order\Storage\OrderFileStorage;
$fileName = $argv[1];
$minimumAmount = (int) file_get_contents('input/minimumAmount');
$orderFactory = new OrderFactory();
$logger = new FileLogger('orderProcessLog');
$storage = new OrderFileStorage('result');
$orderProcessor = new OrderProcessor(
new ErrorFactory(),
$orderFactory,
new OrderProcessorResultFactory,
new OrderValidator($minimumAmount),
$logger
);
$fileHandler = fopen($fileName, 'r+');
while ($fields = fgetcsv($fileHandler)) {
$order = $orderFactory->create(
(int) $fields[0],
false,
(string) $fields[1],
array_map('intval', explode(',', $fields[2])),
(float) $fields[3]
);
$result = $orderProcessor->process($order);
if (null !== $error = $result->getError()) {
$logger->log($error->getMessage());
continue;
}
$storage->save($result->getOrder());
}
fclose($fileHandler);