ExperimentsController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 8.59 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 6
dl 11
loc 128
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFullExperimentsAction() 0 19 2
B getTargetsAction() 0 29 3
A getClientsByTypesAction() 0 12 2
A toggleAction() 11 11 2
A getClientsByTypes() 0 17 3
A listAction() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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