Completed
Push — master ( 25df9e...79e94f )
by Kirill
04:19
created

NooliteController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 166
Duplicated Lines 30.72 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 7
dl 51
loc 166
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F putAction() 41 129 24
A sendResponse() 10 10 2

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