Passed
Push — master ( 18c5a8...6b02b9 )
by Nico
11:10
created

TradePostStorageManager::upperStorage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 19
ccs 13
cts 14
cp 0.9286
crap 3.0032
rs 9.8333
c 0
b 0
f 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->storageSum could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method set() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
                $this->storage->/** @scrutinizer ignore-call */ 
74
                                set($storage->getCommodityId(), $storage);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            }
75
        }
76
77 7
        return $this->storage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->storage could return the type null which is incompatible with the type-hinted return Doctrine\Common\Collections\Collection. Consider adding an additional type-check to rule them out.
Loading history...
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