Passed
Pull Request — master (#1884)
by Janko
50:38 queued 25:12
created

AbstractSystemData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 26.67%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 34
ccs 4
cts 15
cp 0.2667
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setShip() 0 3 1
A __construct() 0 4 1
A update() 0 5 1
A getStatusBar() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Ship\System\Data;
6
7
use Stu\Component\Ship\System\ShipSystemTypeEnum;
8
use Stu\Module\Template\StatusBarFactoryInterface;
9
use Stu\Module\Template\StatusBarInterface;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Repository\ShipSystemRepositoryInterface;
12
13
abstract class AbstractSystemData
14
{
15
    protected ShipInterface $ship;
16
17 5
    public function __construct(
18
        private ShipSystemRepositoryInterface $shipSystemRepository,
19
        private StatusBarFactoryInterface $statusBarFactory
20 5
    ) {}
21
22 3
    public function setShip(ShipInterface $ship): void
23
    {
24 3
        $this->ship = $ship;
25
    }
26
27
    abstract public function getSystemType(): ShipSystemTypeEnum;
28
29
    /**
30
     * updates the system metadata for this specific ship system
31
     */
32
    public function update(): void
33
    {
34
        $system = $this->ship->getShipSystem($this->getSystemType());
35
        $system->setData(json_encode($this, JSON_THROW_ON_ERROR));
36
        $this->shipSystemRepository->save($system);
37
    }
38
39
    protected function getStatusBar(string $label, int $value, int $maxValue, string $color): StatusBarInterface
40
    {
41
        return $this->statusBarFactory
42
            ->createStatusBar()
43
            ->setColor($color)
44
            ->setLabel($label)
45
            ->setMaxValue($maxValue)
46
            ->setValue($value);
47
    }
48
}
49