NooliteController::putAction()   F
last analyzed

Complexity

Conditions 29
Paths 3587

Size

Total Lines 134
Code Lines 70

Duplication

Lines 44
Ratio 32.84 %

Importance

Changes 0
Metric Value
dl 44
loc 134
rs 2
c 0
b 0
f 0
cc 29
eloc 70
nc 3587
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Action;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Annotation\Route;
10
11
class NooliteController extends Controller
12
{
13
14
    private $channelMap = [
15
16
        1 => ['name'=>'noolite.main', 'source'=>'human'],
17
        2 => ['name'=>'noolite.kitchen', 'source'=>'human'],
18
        3 => ['name'=>'noolite.diningroom', 'source'=>'human'],
19
        4 => ['name'=>'noolite.main', 'source'=>'pi'],
20
        5 => ['name'=>'noolite.toilet', 'source'=>'pi'],
21
        6 => ['name'=>'noolite.diningroom', 'source'=>'pi'],
22
        7 => ['name'=>'noolite.kitchen', 'source'=>'pi'],
23
        9 => ['name'=>'noolite.toilet', 'source'=>'human,upper'],
24
        10 => ['name'=>'noolite.toilet', 'source'=>'human,lower'],
25
    ];
26
27
    /**
28
     * @Route("/noolite", methods={"GET"})
29
     * @param Request $request
30
     * @return Response
31
     * @throws \Exception
32
     */
33
    public function putAction(Request $request)
34
    {
35
36
        $channel = $request->get('ch');
37
38
        if (!$channel) {
39
            return $this->sendResponse(false, ['message'=>'No channel is set']);
40
        }
41
42
43
        if (isset($this->channelMap[$channel])) {
44
            $device = $this->
45
                        getDoctrine()->
46
                        getRepository('AppBundle:Device')->
47
                        findOneBy(['alias'=>$this->channelMap[$channel]['name']]);
48
49
            if (!$device) {
50
                return $this->sendResponse(
51
                    false,
52
                    ['message'=>'Device "'.$this->channelMap[$channel]['name'].'" not found']
53
                );
54
            }
55
56
            $actions = $device->getActions();
57
58
59
            $resultAction = null;
60
            $changeSet = [];
61
62
63
            if ($request->get('cmd') == 4) {
64
                // reverse state, only by human
65
66
                $oldState = $device->getState();
67
                $oldState = $oldState['state'];
68
69
                if ($oldState == 'off') {
70
                    // turning on
71
                    $changeSet = ['state'=>'on'];
72
73
                    /** @var Action $action */
74
                    foreach ($actions as $action) {
75
                        $args = json_decode($action->getArguments(), true);
76
                        if (isset($args['state']) && ($args['state'] == 'on')) {
77
                            $resultAction = $action;
78
                            break;
79
                        }
80
                    }
81 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
                    // turning off
83
                    $changeSet = ['state'=>'off'];
84
                    /** @var Action $action */
85
                    foreach ($actions as $action) {
86
                        $args = json_decode($action->getArguments(), true);
87
                        if (isset($args['state']) && ($args['state'] == 'off')) {
88
                            $resultAction = $action;
89
                            break;
90
                        }
91
                    }
92
                }
93
            }
94
95 View Code Duplication
            if ($request->get('cmd') == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
                // turn off, only by pi
97
98
                $changeSet = ['state'=>'off'];
99
                /** @var Action $action */
100
                foreach ($actions as $action) {
101
                    $args = json_decode($action->getArguments(), true);
102
                    if (isset($args['state']) && ($args['state'] == 'off')) {
103
                        $resultAction = $action;
104
                        break;
105
                    }
106
                }
107
            }
108
109
            if ($request->get('cmd') == 6) {
110
                // turn off, only by pi
111
112
                $percent = round((($request->get('d0') - 34)/123)*100);
113
114
                $changeSet = ['state'=>'on', 'percent'=>$percent];
115
                /** @var Action $action */
116
                foreach ($actions as $action) {
117
                    $args = json_decode($action->getArguments(), true);
118
                    if (isset($args['state']) && ($args['state'] == 'on')) {
119
                        $resultAction = $action;
120
                        break;
121
                    }
122
                }
123
            }
124
125 View Code Duplication
            if ($request->get('cmd') == 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126
                // turn on, only by pi
127
128
                $changeSet = ['state'=>'on'];
129
                /** @var Action $action */
130
                foreach ($actions as $action) {
131
                    $args = json_decode($action->getArguments(), true);
132
                    if (isset($args['state']) && ($args['state'] == 'on')) {
133
                        $resultAction = $action;
134
                        break;
135
                    }
136
                }
137
            }
138
139 View Code Duplication
            if (($changeSet['state'] == 'off') && !isset($changeSet['percent'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
140
                $changeSet['percent'] = 0;
141
            }
142
143 View Code Duplication
            if (($changeSet['state'] == 'on') && !isset($changeSet['percent'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
144
                $changeSet['percent'] = 100; // ? I don't really know if they restore their states
145
            }
146
147
            if (!$resultAction) {
148
                return $this->sendResponse(true, ['message'=>'State was changed, but no action was found']);
149
            }
150
151
            $this->get('actions')->executeVirtual($resultAction, $this->channelMap[$channel]['source'], $changeSet);
152
153
            $this->getDoctrine()->getManager()->flush();
154
155
            return $this->sendResponse(
156
                true,
157
                [
158
                    'message'=>'Executed successfully',
159
                    'device'=>$device->getAlias(),
160
                    'changeset'=>$changeSet
161
                ]
162
            );
163
        } else {
164
            return $this->sendResponse(false, ['message'=>'Requested channel does not exist']);
165
        }
166
    }
167
168
    /**
169
     * @param $success
170
     * @param array $resp
171
     * @return Response
172
     */
173 View Code Duplication
    public function sendResponse($success, array $resp)
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...
174
    {
175
        $response = new Response();
176
        $response->setContent(json_encode(array(
177
            'result' => $success ? 'ok' : 'fail',
178
            'response'=> $resp,
179
        )));
180
        $response->headers->set('Content-Type', 'application/json');
181
        return $response;
182
    }
183
}
184