1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awin\ReportTask\Bundle\ReportBundle\Service; |
4
|
|
|
|
5
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Service\Observer\ReportObserver; |
6
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Service\Observer\ReportObserverInterface; |
7
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Model\TransactionStorageInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The report service class for generating the report. |
11
|
|
|
* It use observer pattern to convert the currencies, |
12
|
|
|
* it also could introduce other observers such as email, fraud-check validation alert, etc |
13
|
|
|
* |
14
|
|
|
* @date 24/06/2017 |
15
|
|
|
* @time 20:18 |
16
|
|
|
* @author Peng Yue <[email protected]> |
17
|
|
|
* @copyright 2004-2017 Peng Yue |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class ReportService implements ReportServiceInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array|ReportObserverInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $observers = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The report csv file path |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $reportFilePath = 'var/storage/report.csv'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Generate the report |
36
|
|
|
* |
37
|
|
|
* @param MerchantTransactionServiceInterface $merchantTransactionService |
38
|
|
|
* @param CurrencyServiceInterface $currencyService |
39
|
|
|
* @param TransactionStorageInterface $transactionStorage |
40
|
|
|
* @param int|null $merchantId |
41
|
|
|
* @param string|null $date |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
2 |
|
public function generate( |
46
|
|
|
MerchantTransactionServiceInterface $merchantTransactionService, |
47
|
|
|
CurrencyServiceInterface $currencyService, |
48
|
|
|
TransactionStorageInterface $transactionStorage, |
49
|
|
|
$merchantId, |
50
|
|
|
$date = null |
51
|
|
|
) { |
52
|
|
|
$data = $merchantTransactionService |
53
|
2 |
|
->filterTransactionsByMerchantId($merchantId) |
54
|
2 |
|
->filterTransactionsByDate($date) |
55
|
2 |
|
->getReportData(); |
56
|
2 |
|
$results = $this->notify($data, $currencyService); |
57
|
|
|
|
58
|
2 |
|
return $transactionStorage->setData($results)->save($this->getReportFilePath()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the report file path |
63
|
|
|
* |
64
|
|
|
* @param string $filePath |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
2 |
|
public function setReportFilePath($filePath) |
69
|
|
|
{ |
70
|
2 |
|
$this->reportFilePath = $filePath; |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the csv file path |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
2 |
|
public function getReportFilePath() |
81
|
|
|
{ |
82
|
2 |
|
return $this->reportFilePath; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Attach an observer for report generation |
87
|
|
|
* |
88
|
|
|
* @param ReportObserverInterface $observer |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
4 |
|
public function attach(ReportObserverInterface $observer) |
93
|
|
|
{ |
94
|
4 |
|
$this->observers[] = $observer; |
95
|
|
|
|
96
|
4 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* detach an observer for report generation |
101
|
|
|
* |
102
|
|
|
* @param ReportObserverInterface $observer |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
1 |
|
public function detach(ReportObserverInterface $observer) |
107
|
|
|
{ |
108
|
1 |
|
$key = array_search($observer, $this->observers); |
109
|
|
|
|
110
|
1 |
|
unset($this->observers[$key]); |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $data |
117
|
|
|
* @param CurrencyServiceInterface $currencyService |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
3 |
|
public function notify(array $data, CurrencyServiceInterface $currencyService) |
122
|
|
|
{ |
123
|
3 |
|
$result = []; |
124
|
3 |
|
foreach ($this->observers as $observer) { |
125
|
3 |
|
$result = $observer->listenReportGeneration($data, $currencyService); |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
return $result; |
129
|
|
|
} |
130
|
|
|
} |