Test Failed
Pull Request — master (#7)
by Thomas
05:50
created

StandardVariation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 53
ccs 9
cts 13
cp 0.6923
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
A getName() 0 4 1
A getPercentage() 0 6 3
A run() 0 6 1
1
<?php
2
/**
3
 * InnoCraft Ltd - We are the makers of Piwik Analytics, the leading open source analytics platform.
4
 *
5
 * @link https://www.innocraft.com
6
 * @license https://www.gnu.org/licenses/lgpl-3.0.en.html LGPL v3.0
7
 */
8
9
namespace InnoCraft\Experiments\Variations;
10
11
use InvalidArgumentException;
12
13
class StandardVariation implements VariationInterface {
14
15
    /**
16
     * @var array
17
     */
18
    protected $variation;
19
20
    /**
21
     * @param array $variation eg array('name' => 'blueColor', 'percentage' => 50).
22
     *                            A name has to be given and can be also an ID, eg "4". Percentage is optional.
23
     *                            If given, it defines how much traffic this variation should get. For example defining
24
     *                            50 means, this variation will be activated in 50% of overall experiment activations.
25
     *
26
     * @throws InvalidArgumentException If no variation is given
27
     */
28 19
    public function __construct($variation)
29
    {
30 19
        if (!isset($variation['name']) || $variation['name'] === false || $variation['name'] === '') {
31
            throw new InvalidArgumentException('No variation name is given');
32
        }
33
34 19
        $this->variation = $variation;
35 19
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 10
    public function getName()
41
    {
42 10
        return $this->variation['name'];
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 2
    public function getPercentage()
49
    {
50 2
        if (isset($this->variation['percentage']) && $this->variation['percentage'] !== false) {
51
            return (int) $this->variation['percentage'];
52
        }
53 2
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function run()
59
    {
60
        // we do nothing by default. Usually it is used to get the name of the variation and then do some logic based
61
        // on it. Eg:
62
        // if ($activatedVariation->getName() == 'blueColor') { echo 'blue'; } else { echo 'green'; }
63
    }
64
65
}