Passed
Push — master ( 66afc8...ff3c83 )
by Janko
09:01
created

StorageManager::setOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
crap 1.0028
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer\Storage;
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 RuntimeException;
9
use Stu\Lib\Transfer\EntityWithStorageInterface;
10
use Stu\Lib\Transfer\Storage\Exception\CommodityMissingException;
11
use Stu\Lib\Transfer\Storage\Exception\QuantityTooSmallException;
12
use Stu\Lib\Transfer\TransferEntityTypeEnum;
13
use Stu\Orm\Entity\Commodity;
14
use Stu\Orm\Entity\Storage;
15
use Stu\Orm\Repository\StorageRepositoryInterface;
16
17
final class StorageManager implements StorageManagerInterface
18
{
19 10
    public function __construct(
20
        private readonly StorageRepositoryInterface $storageRepository
21 10
    ) {}
22
23 4
    #[Override]
24
    public function lowerStorage(EntityWithStorageInterface $entity, Commodity $commodity, int $amount): void
25
    {
26 4
        $storageList = $entity->getStorage();
27
28 4
        $storage = $storageList[$commodity->getId()] ?? null;
29 4
        if ($storage === null) {
30 1
            throw new CommodityMissingException();
31
        }
32
33 3
        $storedAmount = $storage->getAmount();
34 3
        if ($storedAmount < $amount) {
35 1
            throw new QuantityTooSmallException(
36 1
                sprintf(
37 1
                    _('Tried to lower commodityId %d (%s) on entityId %d (%s) by %d, but only %d stored.'),
38 1
                    $commodity->getId(),
39 1
                    $commodity->getName(),
40 1
                    $entity->getId(),
41 1
                    $entity->getTransferEntityType()->value,
42 1
                    $amount,
43 1
                    $storedAmount
44 1
                )
45 1
            );
46
        }
47
48 2
        if ($storedAmount === $amount) {
49 1
            $storageList->removeElement($storage);
50 1
            $this->storageRepository->delete($storage);
51
52 1
            return;
53
        }
54
55 1
        $storage->setAmount($storedAmount - $amount);
56
57 1
        $this->storageRepository->save($storage);
58
    }
59
60 4
    #[Override]
61
    public function upperStorage(EntityWithStorageInterface $entity, Commodity $commodity, int $amount): void
62
    {
63 4
        $storages = $entity->getStorage();
64 4
        $commodityId = $commodity->getId();
65
66 4
        $storage = $storages[$commodityId] ?? null;
67
68 4
        if ($storage === null) {
69 3
            $user = $entity->getUser();
70 3
            if ($user === null) {
71
                throw new RuntimeException('this should not happen');
72
            }
73 3
            $storage = $this->storageRepository->prototype()
74 3
                ->setUser($user)
75 3
                ->setCommodity($commodity);
76
77 3
            $this->setOwner($storage, $entity);
78
79 3
            $storages->set($commodityId, $storage);
80
        }
81
82 4
        $storage->setAmount($storage->getAmount() + $amount);
83
84 4
        $this->storageRepository->save($storage);
85
    }
86
87 3
    private function setOwner(Storage $storage, EntityWithStorageInterface $entity): void
88
    {
89 3
        $entityType = $entity->getTransferEntityType();
90
91 3
        match ($entityType) {
92 3
            TransferEntityTypeEnum::COLONY,
93 3
            TransferEntityTypeEnum::SHIP,
94 3
            TransferEntityTypeEnum::STATION => $storage->setEntity($entity),
95
            default => throw new RuntimeException(sprintf('unsupported setter for type %s', $entityType->value))
96 3
        };
97
    }
98
}
99