Test Failed
Pull Request — master (#20)
by Rhodri
03:16
created

StandardChance::getPercentage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Scientist\Chances;
3
4
use RandomLib\Factory;
5
use RandomLib\Generator;
6
7
class StandardChance implements Chance
8
{
9
    private $generator;
10
11
    private $percentage = 100;
12
13
    /**
14
     * StandardChance constructor.
15
     * @param Generator|null $generator
16
     */
17
    public function __construct(Generator $generator = null)
18
    {
19
        if ($generator === null) {
20
            $factory = new Factory;
21
            $generator = $factory->getLowStrengthGenerator();
22
        }
23
        $this->generator = $generator;
24
    }
25
26
    /**
27
     * Determine whether or not the experiment should run
28
     */
29
    public function shouldRun()
30
    {
31
        if ($this->percentage == 0) {
32
            return false;
33
        }
34
35
        $random = $this->generator
36
            ->generateInt(0, 100);
37
        return $random <= $this->percentage;
38
    }
39
40
    /**
41
     * @return int
42
     */
43
    public function getPercentage()
44
    {
45
        return $this->percentage;
46
    }
47
48
    /**
49
     * @param int $percentage
50
     * @return $this
51
     */
52
    public function setPercentage($percentage)
53
    {
54
        $this->percentage = $percentage;
55
        return $this;
56
    }
57
}
58