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

SpacecraftTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 19
ccs 7
cts 9
cp 0.7778
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getThis() 0 7 2
A getSpacecraftSystem() 0 8 2
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