Completed
Push — master ( 2322ba...25df9e )
by Kirill
04:32
created

NooliteController::putAction()   D

Complexity

Conditions 17
Paths 227

Size

Total Lines 107
Code Lines 54

Duplication

Lines 35
Ratio 32.71 %

Importance

Changes 0
Metric Value
dl 35
loc 107
rs 4.0798
c 0
b 0
f 0
cc 17
eloc 54
nc 227
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
    ];
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 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...
105
                // turn on, only by pi
106
107
                $changeSet = ['state'=>'on'];
108
                /** @var Action $action */
109
                foreach ($actions as $action) {
110
                    if ($action->getArguments()=='{"state":"on"}') {
111
                        $resultAction = $action;
112
                        break;
113
                    }
114
                }
115
            }
116
117
118
            if (!$resultAction) {
119
                return $this->sendResponse(true, ['message'=>'State was changed, but no action was found']);
120
            }
121
122
            $this->get('actions')->executeVirtual($resultAction, $this->channelMap[$channel]['source'], $changeSet);
123
124
            $this->getDoctrine()->getManager()->flush();
125
126
            return $this->sendResponse(
127
                true,
128
                [
129
                    'message'=>'Executed successfully',
130
                    'device'=>$device->getAlias(),
131
                    'changeset'=>$changeSet
132
                ]
133
            );
134
        } else {
135
            return $this->sendResponse(false, ['message'=>'Requested channel does not exist']);
136
        }
137
    }
138
139
    /**
140
     * @param $success
141
     * @param array $resp
142
     * @return Response
143
     */
144 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...
145
    {
146
        $response = new Response();
147
        $response->setContent(json_encode(array(
148
            'result' => $success ? 'ok' : 'fail',
149
            'response'=> $resp,
150
        )));
151
        $response->headers->set('Content-Type', 'application/json');
152
        return $response;
153
    }
154
}
155