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

BasicTradeAccountWrapper::getShip()   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\Module\Commodity\CommodityTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeEnum 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...
9
use Stu\Orm\Entity\BasicTradeInterface;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Entity\StorageInterface;
12
use Stu\Orm\Entity\TradePostInterface;
13
use Stu\Orm\Repository\CommodityRepositoryInterface;
14
use Stu\Orm\Repository\StorageRepositoryInterface;
15
16
final class BasicTradeAccountWrapper implements BasicTradeAccountWrapperInterface
17
{
18
    /**
19
     * @var array<StorageInterface>|null
20
     */
21
    private ?array $storage = null;
22
23
    /**
24
     * @param array<BasicTradeInterface> $basicTrades
25
     */
26
    public function __construct(private StorageRepositoryInterface $storageRepository, private TradePostInterface $tradePost, private array $basicTrades, private int $userId, private CommodityRepositoryInterface $commodityRepository)
27
    {
28
    }
29
30
    #[Override]
31
    public function getId(): int
32
    {
33
        return $this->tradePost->getId();
34
    }
35
36
    #[Override]
37
    public function getShip(): ShipInterface
38
    {
39
        return $this->tradePost->getShip();
40
    }
41
42
    #[Override]
43
    public function getTradePostDescription(): string
44
    {
45
        return $this->tradePost->getDescription();
46
    }
47
48
    #[Override]
49
    public function getBasicTradeItems(): array
50
    {
51
        $result = [];
52
53
        $storage = $this->getStorage();
54
55
        foreach ($this->basicTrades as $basicTrade) {
56
            $commodityId = $basicTrade->getCommodity()->getId();
57
            $result[] = new BasicTradeItem($basicTrade, $storage[$commodityId] ?? null);
58
        }
59
60
        return $result;
61
    }
62
63
    #[Override]
64
    public function getLatinumItem(): BasicTradeItem
65
    {
66
        $latinumStorage = $this->getStorage()[CommodityTypeEnum::COMMODITY_LATINUM] ?? null;
67
        $latinumCommodity = $this->commodityRepository->find(CommodityTypeEnum::COMMODITY_LATINUM);
68
        return new BasicTradeItem(null, $latinumStorage, $latinumCommodity);
69
    }
70
71
    /**
72
     * @return array<StorageInterface>
73
     */
74
    private function getStorage(): array
75
    {
76
        if ($this->storage === null) {
77
            $this->storage = $this->storageRepository->getByTradePostAndUser(
78
                $this->tradePost->getId(),
79
                $this->userId
80
            );
81
        }
82
        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...
83
    }
84
85
    #[Override]
86
    public function getStorageSum(): int
87
    {
88
        return array_reduce(
89
            $this->getStorage(),
90
            fn (int $value, StorageInterface $storage): int => $value + $storage->getAmount(),
91
            0
92
        );
93
    }
94
95
    #[Override]
96
    public function isOverStorage(): bool
97
    {
98
        return $this->getStorageSum() > $this->tradePost->getStorage();
99
    }
100
101
    #[Override]
102
    public function getStorageCapacity(): int
103
    {
104
        return $this->tradePost->getStorage();
105
    }
106
}
107