1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awin\ReportTask\Bundle\ReportBundle\Model; |
4
|
|
|
|
5
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Exception\ReportFileSaveFailureException; |
6
|
|
|
use League\Csv\Reader; |
7
|
|
|
use League\Csv\Writer; |
8
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The class to read data from CSV file, it is implemented from TransactionStorageInterface |
12
|
|
|
* and other implementation could introduced, for example, database, elasticsearch, API, etc |
13
|
|
|
* |
14
|
|
|
* @date 24/06/2017 |
15
|
|
|
* @time 15:20 |
16
|
|
|
* @author Peng Yue <[email protected]> |
17
|
|
|
* @copyright 2004-2017 Peng Yue |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class TransactionCsvStorage implements TransactionStorageInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The source csv file path |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $csvFilePath; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The data to be save into storage |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $data = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* TransactionCsvStorage constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $csvFilePath |
40
|
|
|
*/ |
41
|
8 |
|
public function __construct($csvFilePath) |
42
|
|
|
{ |
43
|
8 |
|
$this->csvFilePath = $csvFilePath; |
44
|
|
|
|
45
|
8 |
|
if (!ini_get("auto_detect_line_endings")) { |
46
|
2 |
|
ini_set("auto_detect_line_endings", '1'); |
47
|
|
|
} |
48
|
8 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the all the data from source csv |
52
|
|
|
* There is no pagination for simplicity |
53
|
|
|
* |
54
|
|
|
* @param int $offset |
55
|
|
|
* @param int $limit |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
6 |
|
public function getData($offset, $limit) |
60
|
|
|
{ |
61
|
6 |
|
$csv = Reader::createFromPath($this->csvFilePath); |
62
|
6 |
|
$csv->setDelimiter(';'); |
63
|
6 |
|
$data = $csv->setOffset($offset)->setLimit($limit)->fetchAll(); |
64
|
|
|
|
65
|
6 |
|
return $data; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the data for saving into storage |
70
|
|
|
* |
71
|
|
|
* @param array $data |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
3 |
|
public function setData(array $data) |
76
|
|
|
{ |
77
|
3 |
|
$this->data = $data; |
78
|
|
|
|
79
|
3 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Save the data into storage |
84
|
|
|
* |
85
|
|
|
* @param string $targetCsvFile |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
2 |
|
public function save($targetCsvFile) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
2 |
|
if (file_exists($targetCsvFile)) { |
93
|
2 |
|
@unlink($targetCsvFile); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$csv = Writer::createFromPath($targetCsvFile, 'a+'); |
97
|
|
|
|
98
|
2 |
|
$csv->insertOne([ |
99
|
2 |
|
'Merchant ID', |
100
|
|
|
'Date', |
101
|
|
|
'Original Transaction', |
102
|
|
|
'Currency Symbol', |
103
|
|
|
'Transaction Amount', |
104
|
|
|
'Transaction In GBP', |
105
|
|
|
'Transaction In EUR', |
106
|
|
|
'Transaction In USD', |
107
|
|
|
]); |
108
|
|
|
|
109
|
1 |
|
$csv->insertAll($this->data); |
110
|
1 |
|
} catch (\Exception $e) { |
111
|
1 |
|
throw new ReportFileSaveFailureException($targetCsvFile); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: