Completed
Push — master ( 11b917...8a0ee2 )
by Thomas
27s
created

UrlRedirectVariation::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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
class UrlRedirectVariation extends StandardVariation {
12
13
    /**
14
     * @var string|int
15
     */
16
    private $experimentName;
17
18
    /**
19
     * A variation that can perform a redirect when passing a URL and executing the run method.
20
     *
21
     * @param string|int $experimentName
22
     * @param array $variation
23
     */
24 15
    public function __construct($experimentName, $variation)
25
    {
26 15
        parent::__construct($variation);
27
28 15
        $this->experimentName = $experimentName;
29 15
    }
30
31 5
    public function getUrl()
32
    {
33 5
        if (isset($this->variation['url'])) {
34 4
            return $this->variation['url'];
35
        }
36
37 1
        return '';
38
    }
39
40 3
    public function getUrlWithExperimentParameters()
41
    {
42 3
        $url = $this->getUrl();
43
44 3
        if (false === strpos($url, '?')) {
45 2
            $url .= '?';
46 2
        } else {
47 1
            $url .= '&';
48
        }
49
50 3
        $url .= 'pk_abe=' . urlencode($this->experimentName) . '&pk_abv=' . urlencode($this->getName());
51
52 3
        return $url;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function run()
59
    {
60
        if (!headers_sent()) {
61
            // for now we do not throw an exception here if headers were already sent as it could break
62
            // users app and in worst case people would simply instead see the original version if headers
63
            // were already sent so should be fine for now
64
            $url = $this->getUrlWithExperimentParameters();
65
            header('Location: ' . $url, true, 302);
66
            exit;
1 ignored issue
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
67
        }
68
    }
69
}