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

DealAccountWrapper::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
13
final class DealAccountWrapper implements DealAccountWrapperInterface
14
{
15
    /** @var array<StorageInterface> */
16
    private ?array $storage = null;
17
18
    public function __construct(
19
        private StorageRepositoryInterface $storageRepository,
20
        private TradePostInterface $tradePost,
21
        private int $userId
22
    ) {
23
    }
24
25
    #[Override]
26
    public function getId(): int
27
    {
28
        return $this->tradePost->getId();
29
    }
30
31
    #[Override]
32
    public function getShip(): ShipInterface
33
    {
34
        return $this->tradePost->getShip();
35
    }
36
37
    /** @return array<StorageInterface> */
38
    private function getStorage(): array
39
    {
40
        if ($this->storage === null) {
41
            $this->storage = $this->storageRepository->getByTradePostAndUser(
42
                $this->tradePost->getId(),
43
                $this->userId
44
            );
45
        }
46
        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...
47
    }
48
49
    #[Override]
50
    public function getStorageSum(): int
51
    {
52
        return array_reduce(
53
            $this->getStorage(),
54
            fn (int $value, StorageInterface $storage): int => $value + $storage->getAmount(),
55
            0
56
        );
57
    }
58
59
    #[Override]
60
    public function isOverStorage(): bool
61
    {
62
        return $this->getStorageSum() > $this->tradePost->getStorage();
63
    }
64
65
    #[Override]
66
    public function getStorageCapacity(): int
67
    {
68
        return $this->tradePost->getStorage();
69
    }
70
}
71