-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayDatasetRepository.php
126 lines (101 loc) · 3.46 KB
/
ArrayDatasetRepository.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace App\Infrastructure\Repository;
use App\UI\Dto\DatasetRowDto;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use UnexpectedValueException;
class ArrayDatasetRepository implements DatasetRepository
{
/**
* @var ArrayCollection<int, DatasetRowDto>|null
*/
private ?ArrayCollection $sortedByInvoiceDate = null;
/**
* @var string[]|null
*/
private ?array $customers = null;
/**
* @var array<string, DateTimeInterface>|null
*/
private ?array $latestInvoiceDateGroupByCustomer = null;
public function __construct(
/**
* @var ArrayCollection<int, DatasetRowDto>
*/
private readonly ArrayCollection $dataset,
) {
}
public function getLatestInvoiceDate(): ?DateTimeInterface
{
return ($this->getSortedByInvoiceDate()->first() ?: null)?->invoiceDate;
}
/**
* @return array<string, DateTimeInterface>
*/
public function getLatestInvoiceDateGroupByCustomer(): array
{
if (is_null($this->latestInvoiceDateGroupByCustomer)) {
$this->latestInvoiceDateGroupByCustomer = [];
foreach ($this->getSortedByInvoiceDate() as $row) {
if (!isset($this->latestInvoiceDateGroupByCustomer[$row->customerId])) {
$this->latestInvoiceDateGroupByCustomer[$row->customerId] = $row->invoiceDate;
}
}
}
return $this->latestInvoiceDateGroupByCustomer;
}
public function getDifferenceBetweenCustomerLastInvoiceAndLatestInvoiceDate(string $customerId): int
{
$customerLastInvoiceDate = $this->getLatestInvoiceDateGroupByCustomer()[$customerId] ?? null;
if (is_null($customerLastInvoiceDate)) {
throw new UnexpectedValueException(sprintf('Not found customer with id `%s`', $customerId));
}
$lastInvoiceDate = $this->getLatestInvoiceDate();
$interval = $lastInvoiceDate->diff($customerLastInvoiceDate);
return $interval->days;
}
public function countCustomerInvoices(string $customerId): int
{
$invoices = [];
foreach ($this->dataset as $row) {
if ($row->customerId === $customerId && !in_array($row->invoiceNo, $invoices)) {
$invoices[] = $row->invoiceNo;
}
}
return count($invoices);
}
public function sumCustomerTotalPurchase(string $customerId): float
{
$sum = 0.0;
foreach ($this->dataset as $row) {
if ($row->customerId === $customerId) {
$sum += $row->totalPurchase;
}
}
return $sum;
}
public function getCustomers(): array
{
if (is_null($this->customers)) {
$this->customers = array_values(array_unique($this->dataset->map(
fn (DatasetRowDto $row) => $row->customerId
)->toArray()));
}
return $this->customers;
}
/**
* @return ArrayCollection<int, DatasetRowDto>
*/
private function getSortedByInvoiceDate(): ArrayCollection
{
if (is_null($this->sortedByInvoiceDate)) {
$criteria = Criteria::create()
->orderBy(['invoiceDate' => Order::Descending])
;
$this->sortedByInvoiceDate = $this->dataset->matching($criteria);
}
return $this->sortedByInvoiceDate;
}
}