-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
83 lines (66 loc) · 2.48 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
use Carbon\Carbon;
use Danbka\CommissionTask\Entity\Transaction;
use Danbka\CommissionTask\Exception\CommissionFeeManagerException;
use Danbka\CommissionTask\Repository\InMemoryTransactionRepository;
use Danbka\CommissionTask\Service\CommissionFeeManager;
use Danbka\CommissionTask\Service\CurrencyConverter;
use Evp\Component\Money\Money;
require __DIR__.'/vendor/autoload.php';
try {
$params = getopt('', ['file:']);
if (empty($params['file'])) {
throw new Exception('Parameter --file is required');
}
$filePath = $params['file'];
if (!file_exists($filePath)) {
throw new Exception("File doesn't exist");
}
$fp = fopen($filePath, 'r');
if ($fp === false) {
throw new Exception('Could not open the file');
}
$currencies = [
'EUR' => [
'exchangeRate' => 1.0,
'precision' => 2,
],
'USD' => [
'exchangeRate' => 1.1497,
'precision' => 2,
],
'JPY' => [
'exchangeRate' => 129.53,
'precision' => 0,
],
];
// Repository. If it's necessary we will be able to change it
$transactionRepository = new InMemoryTransactionRepository();
$currencyConverter = new CurrencyConverter($currencies);
$commissionFeeManager = new CommissionFeeManager($transactionRepository, $currencyConverter);
// string format: 2014-12-31,4,natural,cash_out,1200.00,EUR
while ($data = fgetcsv($fp)) {
// if something go wrong won't stop the process
try {
$transaction = (new Transaction())
->setDate(Carbon::parse($data[0]))
->setType($data[3])
->setUserId((int) $data[1])
->setUserType($data[2])
->setMoney(new Money($data[4], $data[5]))
;
// add transaction to repository
$transactionRepository->add($transaction);
// and calculate its commission
$commissionFee = $commissionFeeManager->calculate($transaction);
// output format is client's responsibility
fwrite(STDOUT, $commissionFee->ceil(Money::getFraction($commissionFee->getCurrency()))->getAmount().PHP_EOL);
} catch (CommissionFeeManagerException $exception) {
fwrite(STDOUT, $exception->getMessage());
}
}
fclose($fp);
} catch (Exception $exception) {
fwrite(STDOUT, 'Exception occured: '.$exception->getMessage());
}