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

StandardVariation::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
}