|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MonsieurBiz\SyliusSalesReportsPlugin\Event; |
|
6
|
|
|
|
|
7
|
|
|
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\AlreadyExistsReport; |
|
8
|
|
|
use MonsieurBiz\SyliusSalesReportsPlugin\Exception\NotExistsReport; |
|
9
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
11
|
|
|
|
|
12
|
|
|
final class CustomReportEvent extends Event |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $customReports = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ChannelInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $channel; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \DateTimeInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $fromDate; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \DateTimeInterface|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $toDate; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
ChannelInterface $channel, |
|
36
|
|
|
\DateTimeInterface $fromDate, |
|
37
|
|
|
?\DateTimeInterface $toDate = null |
|
38
|
|
|
) { |
|
39
|
|
|
$this->channel = $channel; |
|
40
|
|
|
$this->fromDate = $fromDate; |
|
41
|
|
|
$this->toDate = $toDate; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retrieve custom reports, use a template override to display it |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getCustomReports(): array |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->customReports; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Add a new custom report, you can't have reports with the same key |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* @throws AlreadyExistsReport |
|
60
|
|
|
*/ |
|
61
|
|
|
public function addReport(string $key, array $data) |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset($this->customReports[$key])) { |
|
64
|
|
|
throw new AlreadyExistsReport(sprintf('Report "%s" already exists', $key)); |
|
65
|
|
|
} |
|
66
|
|
|
$this->customReports[$key] = $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Remove a custom report, you cannot remove a key which not exists |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $key |
|
73
|
|
|
* @throws NotExistsReport |
|
74
|
|
|
*/ |
|
75
|
|
|
public function removeReport(string $key) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!isset($this->customReports[$key])) { |
|
78
|
|
|
throw new NotExistsReport(sprintf('Report "%s" does not exist', $key)); |
|
79
|
|
|
} |
|
80
|
|
|
unset($this->customReports[$key]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return ChannelInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getChannel(): ChannelInterface |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->channel; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return \DateTimeInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getFromDate(): \DateTimeInterface |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->fromDate; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return \DateTimeInterface|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getToDate(): ?\DateTimeInterface |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->toDate; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|