Passed
Push — master ( a3e5b5...489d7c )
by Petr
05:59
created

BiomQualifier::qualifyBiom()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Vehsamrak\Terraformator\LocationGenerator;
4
5
use Vehsamrak\Terraformator\Entity\Location;
6
use Vehsamrak\Terraformator\Entity\PreviousLocationMap;
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 9
    public function __construct(RandomGenerator $randomGenerator)
20
    {
21 9
        $this->randomGenerator = $randomGenerator;
22 9
    }
23
24 9
    public function qualifyBiom(PreviousLocationMap $previousLocations): Biom
25
    {
26 9
        $previousLocationsNumber = $previousLocations->count();
27 9
        $newBiomProbability = $previousLocationsNumber > 0 ? 50 - $previousLocationsNumber * 10 : 100;
28 9
        $randomPercent = mt_rand(1, 100);
29
30 9
        if ($randomPercent <= $newBiomProbability) {
31 4
            return $this->getRandomBiom();
32
        } else {
33 7
            $randomLocationKey = array_rand($previousLocations->getKeys());
34
            /** @var Location $randomLocation */
35 7
            $randomLocation = $previousLocations->get($randomLocationKey);
36 7
            return $randomLocation->getBiom();
37
        }
38
    }
39
40 4
    private function getRandomBiom(): Biom
41
    {
42 4
        $allBioms = Biom::values();
43 4
        $randomBiomKey = $this->randomGenerator->getRandomKeyFromArray($allBioms);
44 4
        $randomBiom = $allBioms[$randomBiomKey];
45
46 4
        return $randomBiom;
47
    }
48
}
49