Passed
Pull Request — master (#2331)
by Janko
11:37 queued 06:03
created

ShipLoader::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib;
6
7
use RuntimeException;
8
use Stu\Module\Spacecraft\Lib\SourceAndTargetWrappersInterface;
9
use Stu\Module\Spacecraft\Lib\SpacecraftLoaderInterface;
10
use Stu\Orm\Entity\Ship;
11
12
final class ShipLoader implements ShipLoaderInterface
13
{
14
    /**
15
     * @param SpacecraftLoaderInterface<ShipWrapperInterface> $spacecraftLoader
16
     */
17 1
    public function __construct(
18
        private SpacecraftLoaderInterface $spacecraftLoader
19 1
    ) {}
20
21 15
    #[\Override]
22
    public function getByIdAndUser(
23
        int $shipId,
24
        int $userId,
25
        bool $allowUplink = false,
26
        bool $checkForEntityLock = true
27
    ): Ship {
28 15
        $spacecraft = $this->spacecraftLoader->getByIdAndUser(
29 15
            $shipId,
30 15
            $userId,
31 15
            $allowUplink,
32 15
            $checkForEntityLock
33 15
        );
34
35 15
        if (!$spacecraft instanceof Ship) {
36
            throw new RuntimeException(sprintf('shipId %d is not a ship', $shipId));
37
        }
38
39 15
        return $spacecraft;
40
    }
41
42 15
    #[\Override]
43
    public function getWrapperByIdAndUser(
44
        int $shipId,
45
        int $userId,
46
        bool $allowUplink = false,
47
        bool $checkForEntityLock = true
48
    ): ShipWrapperInterface {
49 15
        $wrapper = $this->spacecraftLoader->getWrapperByIdAndUser(
50 15
            $shipId,
51 15
            $userId,
52 15
            $allowUplink,
53 15
            $checkForEntityLock
54 15
        );
55
56 15
        if (!$wrapper instanceof ShipWrapperInterface) {
57
            throw new RuntimeException(sprintf('shipId %d is not a ship', $shipId));
58
        }
59
60 15
        return $wrapper;
61
    }
62
63 6
    #[\Override]
64
    public function getWrappersBySourceAndUserAndTarget(
65
        int $shipId,
66
        int $userId,
67
        int $targetId,
68
        bool $allowUplink = false,
69
        bool $checkForEntityLock = true
70
    ): SourceAndTargetWrappersInterface {
71 6
        return $this->spacecraftLoader->getWrappersBySourceAndUserAndTarget(
72 6
            $shipId,
73 6
            $userId,
74 6
            $targetId,
75 6
            $allowUplink,
76 6
            $checkForEntityLock
77 6
        );
78
    }
79
80 2
    #[\Override]
81
    public function find(int $shipId, bool $checkForEntityLock = true): ?ShipWrapperInterface
82
    {
83 2
        $wrapper = $this->spacecraftLoader->find(
84 2
            $shipId,
85 2
            $checkForEntityLock
86 2
        );
87
88 2
        if ($wrapper !== null && !$wrapper instanceof ShipWrapperInterface) {
89
            throw new RuntimeException(sprintf('shipId %d is not a ship', $shipId));
90
        }
91
92 2
        return $wrapper;
93
    }
94
}
95