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

GenerateEmptySystems   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 18
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 23 4
A __construct() 0 10 1
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