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

StandardChance   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 51
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A shouldRun() 0 10 2
A getPercentage() 0 4 1
A setPercentage() 0 5 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