Passed
Push — master ( f50aa1...d726cb )
by Nico
57:47 queued 28:23
created

TradeAccountWrapper::isOverStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Trade\Lib;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Orm\Entity\ShipInterface;
9
use Stu\Orm\Entity\StorageInterface;
10
use Stu\Orm\Entity\TradePostInterface;
11
use Stu\Orm\Repository\StorageRepositoryInterface;
12
use Stu\Orm\Repository\TradeLicenseRepositoryInterface;
13
use Stu\Orm\Repository\TradeOfferRepositoryInterface;
14
use Stu\Orm\Repository\TradeTransferRepositoryInterface;
15
16
final class TradeAccountWrapper implements TradeAccountWrapperInterface
17
{
18
    /**
19
     * @var StorageInterface[]|null
20
     */
21
    private ?array $storage = null;
22
23
    public function __construct(private TradeLicenseRepositoryInterface $tradeLicenseRepository, private TradeTransferRepositoryInterface $tradeTransferRepository, private TradeOfferRepositoryInterface $tradeOfferRepository, private StorageRepositoryInterface $storageRepository, private TradePostInterface $tradePost, private int $userId)
24
    {
25
    }
26
27
    #[Override]
28
    public function getId(): int
29
    {
30
        return $this->tradePost->getId();
31
    }
32
33
    #[Override]
34
    public function getShip(): ShipInterface
35
    {
36
        return $this->tradePost->getShip();
37
    }
38
39
    #[Override]
40
    public function getTradePostDescription(): string
41
    {
42
        return $this->tradePost->getDescription();
43
    }
44
45
    #[Override]
46
    public function getTradePostName(): string
47
    {
48
        return $this->tradePost->getName();
49
    }
50
51
    #[Override]
52
    public function getTradePostbyUser(): bool
53
    {
54
        return $this->tradePost->getUserId() === $this->userId;
55
    }
56
57
    #[Override]
58
    public function getTradePostIsNPC(): bool
59
    {
60
        return $this->tradePost->getUser()->isNpc();
61
    }
62
63
    #[Override]
64
    public function getStorage(): array
65
    {
66
        if ($this->storage === null) {
67
            $this->storage = $this->storageRepository->getByTradePostAndUser(
68
                $this->tradePost->getId(),
69
                $this->userId
70
            );
71
        }
72
        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 array. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    #[Override]
76
    public function getStorageSum(): int
77
    {
78
        return array_reduce(
79
            $this->getStorage(),
80
            fn (int $value, StorageInterface $storage): int => $value + $storage->getAmount(),
81
            0
82
        );
83
    }
84
85
    #[Override]
86
    public function getOfferStorage(): array
87
    {
88
        return $this->tradeOfferRepository->getGroupedSumByTradePostAndUser(
89
            $this->tradePost->getId(),
90
            $this->userId
91
        );
92
    }
93
94
    #[Override]
95
    public function getTradeNetwork(): int
96
    {
97
        return $this->tradePost->getTradeNetwork();
98
    }
99
100
    #[Override]
101
    public function getFreeTransferCapacity(): int
102
    {
103
        return $this->tradePost->getTransferCapacity() - $this->tradeTransferRepository->getSumByPostAndUser(
104
            $this->tradePost->getId(),
105
            $this->userId
106
        );
107
    }
108
109
    #[Override]
110
    public function getTransferCapacity(): int
111
    {
112
        return $this->tradePost->getTransferCapacity();
113
    }
114
115
    #[Override]
116
    public function isOverStorage(): bool
117
    {
118
        return $this->getStorageSum() > $this->tradePost->getStorage();
119
    }
120
121
    #[Override]
122
    public function getStorageCapacity(): int
123
    {
124
        return $this->tradePost->getStorage();
125
    }
126
127
    #[Override]
128
    public function getLicenseCount(): int
129
    {
130
        return $this->tradeLicenseRepository->getAmountByTradePost($this->tradePost->getId());
131
    }
132
133
    #[Override]
134
    public function getFreeStorage(): int
135
    {
136
        return max(0, $this->tradePost->getStorage() - $this->getStorageSum());
137
    }
138
}
139