Passed
Push — dev ( c12b3a...6ad196 )
by Janko
24:31 queued 10:07
created

SpacecraftTrait::getThis()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
namespace Stu\Component\Spacecraft\Trait;
4
5
use RuntimeException;
6
use Stu\Component\Spacecraft\System\Exception\InvalidSystemException;
7
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
8
use Stu\Orm\Entity\SpacecraftInterface;
9
use Stu\Orm\Entity\SpacecraftSystemInterface;
10
11
trait SpacecraftTrait
12
{
13 48
    private function getThis(): SpacecraftInterface
14
    {
15 48
        if (!$this instanceof SpacecraftInterface) {
16
            throw new RuntimeException('trait can only be used on spacecraft entities');
17
        }
18
19 48
        return $this;
20
    }
21
22 20
    public function getSpacecraftSystem(SpacecraftSystemTypeEnum $type): SpacecraftSystemInterface
23
    {
24 20
        $system = $this->getThis()->getSystems()->get($type->value);
25 20
        if ($system === null) {
26
            throw new InvalidSystemException(sprintf('system type %d does not exist on ship', $type->value));
27
        }
28
29 20
        return $system;
30
    }
31
}
32