1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Trade\Lib; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Collections\Collection; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Stu\Orm\Entity\StorageInterface; |
11
|
|
|
use Stu\Orm\Entity\TradePostInterface; |
12
|
|
|
use Stu\Orm\Entity\UserInterface; |
13
|
|
|
use Stu\Orm\Repository\CommodityRepositoryInterface; |
14
|
|
|
use Stu\Orm\Repository\StorageRepositoryInterface; |
15
|
|
|
|
16
|
|
|
final class TradePostStorageManager implements TradePostStorageManagerInterface |
17
|
|
|
{ |
18
|
|
|
private StorageRepositoryInterface $storageRepository; |
19
|
|
|
|
20
|
|
|
private CommodityRepositoryInterface $commodityRepository; |
21
|
|
|
|
22
|
|
|
private TradePostInterface $tradePost; |
23
|
|
|
|
24
|
|
|
private UserInterface $user; |
25
|
|
|
|
26
|
|
|
private ?int $storageSum = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ArrayCollection<int, StorageInterface> |
30
|
|
|
*/ |
31
|
|
|
private ?ArrayCollection $storage = null; |
32
|
|
|
|
33
|
7 |
|
public function __construct( |
34
|
|
|
StorageRepositoryInterface $storageRepository, |
35
|
|
|
CommodityRepositoryInterface $commodityRepository, |
36
|
|
|
TradePostInterface $tradePost, |
37
|
|
|
UserInterface $user |
38
|
|
|
) { |
39
|
7 |
|
$this->storageRepository = $storageRepository; |
40
|
7 |
|
$this->commodityRepository = $commodityRepository; |
41
|
7 |
|
$this->tradePost = $tradePost; |
42
|
7 |
|
$this->user = $user; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getTradePost(): TradePostInterface |
46
|
|
|
{ |
47
|
|
|
return $this->tradePost; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function getStorageSum(): int |
51
|
|
|
{ |
52
|
2 |
|
if ($this->storageSum === null) { |
53
|
2 |
|
$this->storageSum = array_reduce( |
54
|
2 |
|
$this->getStorage()->toArray(), |
55
|
2 |
|
fn (int $value, StorageInterface $storage): int => $value + $storage->getAmount(), |
56
|
2 |
|
0 |
57
|
2 |
|
); |
58
|
|
|
} |
59
|
2 |
|
return $this->storageSum; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function getFreeStorage(): int |
63
|
|
|
{ |
64
|
1 |
|
return max(0, $this->tradePost->getStorage() - $this->getStorageSum()); |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
public function getStorage(): Collection |
68
|
|
|
{ |
69
|
7 |
|
if ($this->storage === null) { |
70
|
7 |
|
$this->storage = new ArrayCollection(); |
71
|
|
|
|
72
|
7 |
|
foreach ($this->storageRepository->getByTradePostAndUser($this->tradePost->getId(), $this->user->getId()) as $storage) { |
73
|
7 |
|
$this->storage->set($storage->getCommodityId(), $storage); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
7 |
|
return $this->storage; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function upperStorage(int $commodityId, int $amount): void |
81
|
|
|
{ |
82
|
2 |
|
$storage = $this->getStorage(); |
83
|
|
|
|
84
|
2 |
|
$stor = $storage->get($commodityId) ?? null; |
85
|
2 |
|
if ($stor === null) { |
86
|
1 |
|
$stor = $this->storageRepository->prototype(); |
87
|
1 |
|
$stor->setUser($this->user); |
88
|
1 |
|
$commodity = $this->commodityRepository->find($commodityId); |
89
|
1 |
|
if ($commodity === null) { |
90
|
|
|
throw new InvalidArgumentException(sprintf('commodityId %d does not exist', $commodityId)); |
91
|
|
|
} |
92
|
1 |
|
$stor->setCommodity($commodity); |
93
|
1 |
|
$stor->setTradePost($this->tradePost); |
94
|
|
|
} |
95
|
2 |
|
$stor->setAmount($stor->getAmount() + $amount); |
96
|
2 |
|
$storage->set($commodityId, $stor); |
97
|
|
|
|
98
|
2 |
|
$this->storageRepository->save($stor); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
public function lowerStorage(int $commodityId, int $amount): void |
102
|
|
|
{ |
103
|
2 |
|
$storage = $this->getStorage(); |
104
|
|
|
|
105
|
2 |
|
$stor = $storage->get($commodityId) ?? null; |
106
|
2 |
|
if ($stor === null) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
if ($stor->getAmount() <= $amount) { |
111
|
1 |
|
$storage->remove($commodityId); |
112
|
1 |
|
$this->storageRepository->delete($stor); |
113
|
1 |
|
return; |
114
|
|
|
} |
115
|
1 |
|
$stor->setAmount($stor->getAmount() - $amount); |
116
|
|
|
|
117
|
1 |
|
$this->storageRepository->save($stor); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|