ExperimentListener   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 93
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C onKernelRequest() 0 62 19
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\EventListener;
13
14
use DeviceDetector\DeviceDetector;
15
use ONGR\CookiesBundle\Cookie\Model\GenericCookie;
16
use ONGR\SettingsBundle\Service\SettingsManager;
17
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18
19
class ExperimentListener
20
{
21
    /**
22
     * @var GenericCookie
23
     */
24
    private $cookie;
25
26
    /**
27
     * @var SettingsManager
28
     */
29
    private $settingsManager;
30
31
    /**
32
     * @var string
33
     */
34
    private $activeExperimentsSettingName;
35
36
    /**
37
     * ExperimentListener constructor.
38
     * @param GenericCookie   $cookie
39
     * @param SettingsManager $settingsManager
40
     * @param string          $settingName
41
     */
42
    public function __construct(GenericCookie $cookie, SettingsManager $settingsManager, $settingName)
43
    {
44
        $this->cookie = $cookie;
45
        $this->settingsManager = $settingsManager;
46
        $this->activeExperimentsSettingName = $settingName;
47
    }
48
49
    public function onKernelRequest(GetResponseEvent $event)
50
    {
51
        if (!$event->isMasterRequest() || (is_array($this->cookie->getValue()) && !empty($this->cookie->getValue()))) {
52
            return;
53
        }
54
55
        $experiments = $this->settingsManager->getActiveExperiments();
56
57
        if (empty($experiments)) {
58
            return;
59
        }
60
61
        $dd = new DeviceDetector($event->getRequest()->headers->get('User-Agent'));
62
        $experimentalProfiles = [];
63
        $dd->parse();
64
65
        foreach ($experiments as $experiment) {
66
            $experiment = $this->settingsManager->getCachedExperiment($experiment);
67
            $targets = json_decode($experiment['value'], true);
68
69
            if (isset($targets['Clients'])) {
70
                if (isset($targets['Clients']['types']) &&
71
                    !in_array(
72
                        strtolower($dd->getClient()['type']),
73
                        array_map('strtolower', $targets['Clients']['types'])
74
                    )
75
                ) {
76
                    continue;
77
                }
78
79
                if (isset($targets['Clients']['clients']) &&
80
                    !in_array($dd->getClient()['name'], $targets['Clients']['clients'])
81
                ) {
82
                    continue;
83
                }
84
            }
85
86
            if (isset($targets['OS']['types']) && !in_array($dd->getOs()['name'], $targets['OS']['types'])) {
87
                continue;
88
            }
89
90
            if (isset($targets['Devices'])) {
91
                if (isset($targets['Devices']['types']) &&
92
                    !in_array($dd->getDeviceName(), $targets['Devices']['types'])
93
                ) {
94
                    continue;
95
                }
96
97
                if (isset($targets['Devices']['brands']) && !in_array($dd->getBrand(), $targets['Devices']['brands'])) {
98
                    continue;
99
                }
100
            }
101
102
            $experimentalProfiles = array_merge($experimentalProfiles, $experiment['profile']);
103
        }
104
105
        if (empty($experimentalProfiles)) {
106
            return;
107
        }
108
109
        $this->cookie->setValue($experimentalProfiles);
110
    }
111
}
112