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