Passed
Pull Request — dev (#2303)
by Janko
05:56
created

Trumfield::getLocation()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\JoinColumn;
14
use Doctrine\ORM\Mapping\ManyToOne;
15
use Doctrine\ORM\Mapping\OneToMany;
16
use Doctrine\ORM\Mapping\OrderBy;
17
use Doctrine\ORM\Mapping\Table;
18
use RuntimeException;
19
use Stu\Lib\Interaction\EntityWithInteractionCheckInterface;
20
use Stu\Lib\Map\EntityWithLocationInterface;
21
use Stu\Lib\Transfer\CommodityTransfer;
22
use Stu\Lib\Transfer\EntityWithStorageInterface;
23
use Stu\Lib\Transfer\TransferEntityTypeEnum;
24
use Stu\Orm\Repository\TrumfieldRepository;
25
26
#[Table(name: 'stu_trumfield')]
27
#[Entity(repositoryClass: TrumfieldRepository::class)]
28
class Trumfield implements
29
    EntityWithStorageInterface,
30
    EntityWithLocationInterface,
31
    EntityWithInteractionCheckInterface
32
{
33
    #[Id]
34
    #[Column(type: 'integer')]
35
    #[GeneratedValue(strategy: 'IDENTITY')]
36
    private int $id;
37
38
    #[Column(type: 'integer', length: 6)]
39
    private int $huelle = 0;
40
41
    #[Column(type: 'integer')]
42
    private int $former_rump_id = 0;
43
44
    #[Column(type: 'integer')]
45
    private int $location_id = 0;
0 ignored issues
show
introduced by
The private property $location_id is not used, and could be removed.
Loading history...
46
47
    /**
48
     * @var ArrayCollection<int, Storage>
49
     */
50
    #[OneToMany(targetEntity: Storage::class, mappedBy: 'trumfield', indexBy: 'commodity_id')]
51
    #[OrderBy(['commodity_id' => 'ASC'])]
52
    private Collection $storage;
53
54
    #[ManyToOne(targetEntity: Location::class)]
55
    #[JoinColumn(name: 'location_id', nullable: false, referencedColumnName: 'id')]
56
    private Location $location;
57
58 2
    #[\Override]
59
    public function getId(): int
60
    {
61 2
        return $this->id;
62
    }
63
64 2
    public function getHull(): int
65
    {
66 2
        return $this->huelle;
67
    }
68
69 1
    public function setHull(int $hull): Trumfield
70
    {
71 1
        $this->huelle = $hull;
72 1
        return $this;
73
    }
74
75 2
    public function getFormerRumpId(): int
76
    {
77 2
        return $this->former_rump_id;
78
    }
79
80
    public function setFormerRumpId(int $formerRumpId): Trumfield
81
    {
82
        $this->former_rump_id = $formerRumpId;
83
        return $this;
84
    }
85
86 1
    #[\Override]
87
    public function getStorage(): Collection
88
    {
89 1
        return $this->storage;
90
    }
91
92 1
    #[\Override]
93
    public function getStorageSum(): int
94
    {
95 1
        return array_reduce(
96 1
            $this->getStorage()->getValues(),
97 1
            fn(int $sum, Storage $storage): int => $sum + $storage->getAmount(),
98 1
            0
99 1
        );
100
    }
101
102 1
    #[\Override]
103
    public function getMaxStorage(): int
104
    {
105 1
        return $this->getStorageSum();
106
    }
107
108 1
    #[\Override]
109
    public function getBeamableStorage(): Collection
110
    {
111 1
        return CommodityTransfer::excludeNonBeamable($this->storage);
112
    }
113
114
    #[\Override]
115
    public function getCrewAssignments(): Collection
116
    {
117
        return new ArrayCollection();
118
    }
119
120 1
    #[\Override]
121
    public function getLocation(): Map|StarSystemMap
122
    {
123 1
        if ($this->location instanceof Map || $this->location instanceof StarSystemMap) {
124 1
            return $this->location;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->location returns the type Stu\Orm\Entity\Location which includes types incompatible with the type-hinted return Stu\Orm\Entity\Map|Stu\Orm\Entity\StarSystemMap.
Loading history...
125
        }
126
127
        throw new RuntimeException('unknown type');
128
    }
129
130
    public function setLocation(Location $location): Trumfield
131
    {
132
        $this->location = $location;
133
134
        return $this;
135
    }
136
137 1
    #[\Override]
138
    public function getUser(): ?User
139
    {
140 1
        return null;
141
    }
142
143 2
    #[\Override]
144
    public function getTransferEntityType(): TransferEntityTypeEnum
145
    {
146 2
        return TransferEntityTypeEnum::TRUMFIELD;
147
    }
148
149 2
    #[\Override]
150
    public function getName(): string
151
    {
152 2
        return $this->getTransferEntityType()->getName();
153
    }
154
155
    #[\Override]
156
    public function getHref(): string
157
    {
158
        return '';
159
    }
160
}
161