Passed
Push — master ( 643140...6d8b29 )
by Janko
26:27
created

AbstractSystemData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
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\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