DashboardController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 170
Duplicated Lines 27.65 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 11
c 4
b 1
f 1
lcom 1
cbo 4
dl 47
loc 170
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 14 1
A addAction() 23 23 2
B editAction() 24 24 2
A wakeupAction() 0 6 1
A suspendAction() 0 6 1
A suspendTask() 0 13 1
A removeAction() 0 11 1
A addFlash() 0 9 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 FOA\CronBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use FOA\CronBundle\Form\Type\CronType;
7
use FOA\CronBundle\Manager\Cron;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
13
/**
14
 * Display dashboard and manage CRUD operations
15
 * @author Novikov Viktor
16
 */
17
class DashboardController extends Controller
18
{
19
    /**
20
     * Displays the current crontab and a form to add a new one.
21
     *
22
     * @AclAncestor("foa_cron_management_index")
23
     *
24
     * @return \Symfony\Component\HttpFoundation\Response
25
     */
26
    public function indexAction()
27
    {
28
        $cronManager = $this->get('foa.cron_bundle.cron_manager');
29
        $this->addFlash('message', $cronManager->getOutput());
30
        $this->addFlash('error', $cronManager->getError());
31
32
        $form = $this->createForm(new CronType(), new Cron());
33
34
        return $this->render('FOACronBundle:Dashboard:index.html.twig', [
35
            'crons' => $cronManager->getCrons(),
36
            'raw'   => $cronManager->getRaw(),
37
            'form'  => $form->createView(),
38
        ]);
39
    }
40
41
    /**
42
     * Add a cron to the cron table
43
     *
44
     * @param Request $request
45
     *
46
     * @return RedirectResponse|Response
47
     */
48 View Code Duplication
    public function addAction(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...
49
    {
50
        $cronManager = $this->get('foa.cron_bundle.cron_manager');
51
        $cron = new Cron();
52
        $this->addFlash('message', $cronManager->getOutput());
53
        $this->addFlash('error', $cronManager->getError());
54
        $form = $this->createForm(new CronType(), $cron);
55
56
        $form->handleRequest($request);
57
        if ($form->isValid()) {
58
            $cronManager->add($cron);
59
            $this->addFlash('message', $cronManager->getOutput());
60
            $this->addFlash('error', $cronManager->getError());
61
62
            return $this->redirect($this->generateUrl('foa_cron_index'));
63
        }
64
65
        return $this->render('FOACronBundle:Dashboard:index.html.twig', [
66
            'crons' => $cronManager->getCrons(),
67
            'raw'   => $cronManager->getRaw(),
68
            'form'  => $form->createView(),
69
        ]);
70
    }
71
72
    /**
73
     * Edit a cron
74
     *
75
     * @param $id - the line of the cron in the cron table
76
     *
77
     * @return RedirectResponse|Response
78
     */
79 View Code Duplication
    public function editAction($id)
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...
80
    {
81
        $cronManager = $this->get('foa.cron_bundle.cron_manager');
82
        $this->addFlash('message', $cronManager->getOutput());
83
        $this->addFlash('error', $cronManager->getError());
84
        $cron = $cronManager->getById($id);
85
        $form = $this->createForm(new CronType(), $cron);
86
87
        $request = $this->get('request');
88
        $form->handleRequest($request);
89
        if ($form->isValid()) {
90
            $cronManager->write();
91
92
            $this->addFlash('message', $cronManager->getOutput());
93
            $this->addFlash('error', $cronManager->getError());
94
95
            return $this->redirect($this->generateUrl('foa_cron_index'));
96
        }
97
98
        return $this->render('FOACronBundle:Dashboard:edit.html.twig', [
99
            'id'   => $id,
100
            'form' => $form->createView(),
101
        ]);
102
    }
103
104
    /**
105
     * Wake up a cron from the cron table
106
     *
107
     * @param $id - the line of the cron in the cron table
108
     *
109
     * @return RedirectResponse
110
     */
111
    public function wakeupAction($id)
112
    {
113
        $this->suspendTask($id, false);
114
115
        return $this->redirect($this->generateUrl('foa_cron_index'));
116
    }
117
118
    /**
119
     * Suspend a cron from the cron table
120
     *
121
     * @param $id - the line of the cron in the cron table
122
     *
123
     * @return RedirectResponse
124
     */
125
    public function suspendAction($id)
126
    {
127
        $this->suspendTask($id, true);
128
129
        return $this->redirect($this->generateUrl('foa_cron_index'));
130
    }
131
132
    /**
133
     * Suspend a task from the cron table
134
     *
135
     * @param int $id - the line of the cron in the cron table
136
     * @param bool $state
137
     */
138
    protected function suspendTask($id, $state)
139
    {
140
        $cronManager = $this->get('foa.cron_bundle.cron_manager');
141
        $this->addFlash('message', $cronManager->getOutput());
142
        $this->addFlash('error', $cronManager->getError());
143
144
        $cron = $cronManager->getById($id);
145
        $cron->setSuspended($state);
146
147
        $cronManager->write();
148
        $this->addFlash('message', $cronManager->getOutput());
149
        $this->addFlash('error', $cronManager->getError());
150
    }
151
152
    /**
153
     * Remove a cron from the cron table
154
     *
155
     * @param $id - the line of the cron in the cron table
156
     *
157
     * @return RedirectResponse
158
     */
159
    public function removeAction($id)
160
    {
161
        $cronManager = $this->get('foa.cron_bundle.cron_manager');
162
        $this->addFlash('message', $cronManager->getOutput());
163
        $this->addFlash('error', $cronManager->getError());
164
        $cronManager->remove($id);
165
        $this->addFlash('message', $cronManager->getOutput());
166
        $this->addFlash('error', $cronManager->getError());
167
168
        return $this->redirect($this->generateUrl('foa_cron_index'));
169
    }
170
171
    /**
172
     * Adds a flash to the flash bag where flashes are array of messages
173
     *
174
     * @param $type
175
     * @param $message
176
     */
177
    protected function addFlash($type, $message)
178
    {
179
        if (empty($message)) {
180
            return;
181
        }
182
183
        $session = $this->get('session');
184
        $session->getFlashBag()->add($type, $message);
185
    }
186
}
187