Completed
Pull Request — master (#21)
by Ben
04:24
created

StandardChance::setPercentage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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 329
    public function __construct(Generator $generator = null)
18
    {
19 329
        if ($generator === null) {
20 23
            $factory = new Factory;
21 23
            $generator = $factory->getLowStrengthGenerator();
22 23
        }
23 329
        $this->generator = $generator;
24 329
    }
25
26
    /**
27
     * Determine whether or not the experiment should run
28
     */
29 307
    public function shouldRun()
30
    {
31 307
        if ($this->percentage == 0) {
32 1
            return false;
33
        }
34
35 306
        $random = $this->generator
36 306
            ->generateInt(0, 100);
37 306
        return $random <= $this->percentage;
38
    }
39
40
    /**
41
     * @return int
42
     */
43 2
    public function getPercentage()
44
    {
45 2
        return $this->percentage;
46
    }
47
48
    /**
49
     * @param int $percentage
50
     * @return $this
51
     */
52 203
    public function setPercentage($percentage)
53
    {
54 203
        $this->percentage = $percentage;
55 203
        return $this;
56
    }
57
}
58