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

StandardChance::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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