Passed
Push — master ( 37217b...efdda9 )
by Nico
19:16 queued 09:28
created

GenerateEmptySystems::generate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 20
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\StarSystem;
6
7
use RuntimeException;
8
use Stu\Component\StarSystem\StarSystemCreationInterface;
9
use Stu\Orm\Repository\LayerRepositoryInterface;
10
use Stu\Orm\Repository\MapRepositoryInterface;
11
use Stu\Orm\Repository\StarSystemRepositoryInterface;
12
13
final class GenerateEmptySystems implements GenerateEmptySystemsInterface
14
{
15
    public const BATCH_AMOUNT = 10;
16
17
    private LayerRepositoryInterface $layerRepository;
18
19
    private MapRepositoryInterface $mapRepository;
20
21
    private StarSystemRepositoryInterface $starSystemRepository;
22
23
    private StarSystemCreationInterface $starSystemCreation;
24
25
    public function __construct(
26
        LayerRepositoryInterface $layerRepository,
27
        MapRepositoryInterface $mapRepository,
28
        StarSystemRepositoryInterface $starSystemRepository,
29
        StarSystemCreationInterface $starSystemCreation
30
    ) {
31
        $this->layerRepository = $layerRepository;
32
        $this->mapRepository = $mapRepository;
33
        $this->starSystemRepository = $starSystemRepository;
34
        $this->starSystemCreation = $starSystemCreation;
35
    }
36
37
    public function generate(int $layerId): int
38
    {
39
        $layer = $this->layerRepository->find($layerId);
40
        if ($layer === null) {
41
            throw new RuntimeException('layer does not exist');
42
        }
43
44
        $mapArray = $this->mapRepository->getWithEmptySystem($layer);
45
46
        $count = 0;
47
48
        $randomNames = $this->starSystemRepository->getRandomFreeSystemNames(self::BATCH_AMOUNT);
49
50
        foreach ($mapArray as $map) {
51
            if ($count === self::BATCH_AMOUNT) {
52
                break;
53
            }
54
55
            $this->starSystemCreation->recreateStarSystem($map,  $randomNames[$count]->getName());
56
            $count++;
57
        }
58
59
        return $count;
60
    }
61
}
62