Completed
Push — master ( b3011a...50a3a2 )
by Walter
02:06
created

Google::getSubscribedEvents()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 42
ccs 22
cts 22
cp 1
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of phpab/phpab. (https://github.com/phpab/phpab)
4
 *
5
 * @link https://github.com/phpab/phpab for the canonical source repository
6
 * @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
7
 * @license https://raw.githubusercontent.com/phpab/phpab/master/LICENSE.md MIT
8
 */
9
10
namespace PhpAb\Analytics\DataCollector;
11
12
use PhpAb\Event\SubscriberInterface;
13
use PhpAb\Test\TestInterface;
14
use PhpAb\Variant\VariantInterface;
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * A data collector that holds information about which tests have been executed in a format for Google.
19
 *
20
 * @package PhpAb
21
 */
22
class Google implements SubscriberInterface
23
{
24
    /**
25
     * @var array Test identifiers and variation indexes
26
     */
27
    private $participations = [];
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function getSubscribedEvents()
33
    {
34
        return [
35 7
            'phpab.participation.variant_run' => function ($options) {
36 7
                Assert::notEmpty($options, 'Array passed to closure cannot be empty.');
37
38 6
                Assert::keyExists($options, 1, 'Second parameter passed to closure must be instance of Bag.');
39
40 5
                Assert::isInstanceOf(
41 5
                    $options[1],
42 5
                    'PhpAb\Test\Bag',
43
                    'Second parameter passed to closure must be instance of Bag.'
44 5
                );
45
46 4
                Assert::keyExists(
47 4
                    $options,
48 4
                    2,
49
                    'Third parameter passed to closure must be instance of VariantInterface.'
50 4
                );
51
52 3
                Assert::isInstanceOf(
53 3
                    $options[2],
54 3
                    'PhpAb\Variant\VariantInterface',
55
                    'Third parameter passed to closure must be instance of VariantInterface.'
56 3
                );
57
58
                /** @var TestInterface $test */
59 2
                $test = $options[1]->getTest();
60
61
                /** @var VariantInterface $chosenVariant */
62 2
                $chosenVariant = $options[2];
63
64 2
                $variants = $test->getVariants();
65
66
                // Get the index number of the element
67 2
                $chosenIndex = array_search($chosenVariant->getIdentifier(), array_keys($variants));
68
69
                // Call the add method
70 2
                $this->addParticipation($test->getIdentifier(), $chosenIndex);
71 2
            }
72 8
        ];
73
    }
74
75
    /**
76
     * Adds a participation to the data collector.
77
     *
78
     * @param string $testIdentifier It will look like "Qp0gahJ3RAO3DJ18b0XoUQ"
79
     * @param int $variationIndex
80
     * @throws InvalidArgumentException
81
     */
82 3
    public function addParticipation($testIdentifier, $variationIndex)
83
    {
84 3
        Assert::string($testIdentifier, 'Test identifier must be a string');
85 3
        Assert::integer($variationIndex, 'Variation index must be integer');
86 3
        Assert::greaterThan($variationIndex, -1, 'Variation index must be integer >= 0');
87
88 3
        $this->participations[$testIdentifier] = $variationIndex;
89 3
    }
90
91
    /**
92
     * Gets the test data that has been collected.
93
     *
94
     * @return array
95
     */
96 3
    public function getTestsData()
97
    {
98 3
        return $this->participations;
99
    }
100
}
101