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\Controller; |
13
|
|
|
|
14
|
|
|
use DeviceDetector\Parser\Device\DeviceParserAbstract; |
15
|
|
|
use DeviceDetector\Parser\OperatingSystem; |
16
|
|
|
use ONGR\SettingsBundle\Document\Setting; |
17
|
|
|
use ONGR\SettingsBundle\Service\SettingsManager; |
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class SettingsListController. Is used for managing settings in General env. |
24
|
|
|
*/ |
25
|
|
|
class ExperimentsController extends Controller |
26
|
|
|
{ |
27
|
|
|
public function listAction(Request $request) |
28
|
|
|
{ |
29
|
|
|
return $this->render('ONGRSettingsBundle:Experiments:list.html.twig'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns a json list of experiments |
34
|
|
|
* |
35
|
|
|
* @return JsonResponse |
36
|
|
|
*/ |
37
|
|
|
public function getFullExperimentsAction() |
38
|
|
|
{ |
39
|
|
|
$experimentsArray = []; |
40
|
|
|
/** @var SettingsManager $manager */ |
41
|
|
|
$manager = $this->get('ongr_settings.settings_manager'); |
42
|
|
|
$experiments = $manager->getAllExperiments(); |
43
|
|
|
$activeExperiments = $manager->getActiveExperiments(); |
44
|
|
|
|
45
|
|
|
/** @var Setting $experiment */ |
46
|
|
|
foreach ($experiments as $experiment) { |
47
|
|
|
$experiment = $experiment->getSerializableData(); |
48
|
|
|
$experiment['active'] = in_array($experiment['name'], $activeExperiments); |
49
|
|
|
$experimentsArray[] = $experiment; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new JsonResponse( |
53
|
|
|
['count' => count($experiments), 'documents' => $experimentsArray] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a json list of targets for experiment |
59
|
|
|
* |
60
|
|
|
* @param Request $request |
61
|
|
|
* |
62
|
|
|
* @return JsonResponse |
63
|
|
|
*/ |
64
|
|
|
public function getTargetsAction(Request $request) |
65
|
|
|
{ |
66
|
|
|
$targets = [ |
67
|
|
|
'Devices' => [ |
68
|
|
|
'types' => DeviceParserAbstract::getAvailableDeviceTypeNames(), |
69
|
|
|
'brands' => DeviceParserAbstract::$deviceBrands, |
70
|
|
|
], |
71
|
|
|
'Clients' => [ |
72
|
|
|
'types' => [ |
73
|
|
|
'Browser', |
74
|
|
|
'FeedReader', |
75
|
|
|
'Library', |
76
|
|
|
'MediaPlayer', |
77
|
|
|
'MobileApp', |
78
|
|
|
'PIM', |
79
|
|
|
], |
80
|
|
|
'clients' => [], |
81
|
|
|
], |
82
|
|
|
'OS' => [ |
83
|
|
|
'types' => OperatingSystem::getAvailableOperatingSystems(), |
84
|
|
|
] |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
if (!empty($select = json_decode($request->get('selected'), true)) && isset($select['Clients']['types'])) { |
88
|
|
|
$targets['Clients']['clients'] = $this->getClientsByTypes($select['Clients']['types']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return new JsonResponse($targets); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a json list of targets for experiment |
96
|
|
|
* |
97
|
|
|
* @param Request $request |
98
|
|
|
* |
99
|
|
|
* @return JsonResponse |
100
|
|
|
*/ |
101
|
|
|
public function getClientsByTypesAction(Request $request) |
102
|
|
|
{ |
103
|
|
|
$types = $request->get('types'); |
104
|
|
|
|
105
|
|
|
if (!$types) { |
106
|
|
|
return new JsonResponse(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$clients = $this->getClientsByTypes($types); |
110
|
|
|
|
111
|
|
|
return new JsonResponse($clients); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Request $request |
116
|
|
|
* |
117
|
|
|
* @return JsonResponse |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function toggleAction(Request $request) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$name = $request->get('name'); |
122
|
|
|
try { |
123
|
|
|
$this->get('ongr_settings.settings_manager')->toggleExperiment($name); |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
return new JsonResponse(['error' => true]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new JsonResponse(['error' => false]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $types |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function getClientsByTypes(array $types) |
136
|
|
|
{ |
137
|
|
|
$clients = []; |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
foreach ($types as $type) { |
141
|
|
|
$clients = array_merge( |
142
|
|
|
("\\DeviceDetector\\Parser\\Client\\$type")::getAvailableClients(), |
143
|
|
|
$clients |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
} catch (\Exception $e) { |
147
|
|
|
return []; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $clients; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.