Passed
Push — master ( e3f202...b2ed8f )
by Petr
05:41
created

BiomQualifier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Vehsamrak\Terraformator\LocationGenerator;
4
5
use Vehsamrak\Terraformator\Entity\Location;
6
use Vehsamrak\Terraformator\Entity\Map;
7
use Vehsamrak\Terraformator\Enum\Biom;
8
use Vehsamrak\Terraformator\Service\RandomGenerator;
9
10
/**
11
 * @author Vehsamrak
12
 */
13
class BiomQualifier
14
{
15
16
    /** @var RandomGenerator $randomGenerator */
17
    private $randomGenerator;
18
19 6
    public function __construct(RandomGenerator $randomGenerator)
20
    {
21 6
        $this->randomGenerator = $randomGenerator;
22 6
    }
23
24 6
    public function qualifyBiom(Map $map): Biom
25
    {
26 6
        $firstLocationInMap = $map->first();
27
28 6
        if (!$firstLocationInMap) {
29 4
            return $this->getRandomBiom();
30
        }
31
32 4
        $firstBiom = $firstLocationInMap->getBiom();
33 4
        $locationsAreSame = true;
34
35
        /** @var Location $location */
36 4
        foreach ($map->toArray() as $location) {
37 4
            if (!$location->getBiom()->equals($firstBiom)) {
38 4
                $locationsAreSame = false;
39
            }
40
        }
41
42 4
        if ($map->count() == 8 && $locationsAreSame) {
43 2
            return $firstBiom;
44
        }
45
46 2
        return $this->getRandomBiom();
47
    }
48
49 4
    private function getRandomBiom(): Biom
50
    {
51 4
        $allBioms = Biom::values();
52 4
        $randomBiomKey = $this->randomGenerator->getRandomKeyFromArray($allBioms);
53 4
        $randomBiom = $allBioms[$randomBiomKey];
54
55 4
        return $randomBiom;
56
    }
57
}
58