Completed
Push — develop ( e21cd6...0a03ac )
by Alexandr
10s
created

DashboardController::editAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 24
rs 8.9713
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
3
namespace FOA\CronBundle\Controller;
4
5
use Symfony\Component\Serializer\Encoder\JsonEncoder;
6
use Symfony\Component\Serializer\Serializer;
7
use Symfony\Component\HttpFoundation\Response;
8
use FOA\CronBundle\Form\Type\CronType;
9
use FOA\CronBundle\Manager\Cron;
10
use FOA\CronBundle\Manager\CronManager;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
16
/**
17
 * Display dashboard and manage CRUD operations
18
 */
19
class DashboardController extends Controller
20
{
21
    /**
22
     * Displays the current crontab and a form to add a new one.
23
     *
24
     * @AclAncestor("foa_cron_management_index")
25
     *
26
     * @return \Symfony\Component\HttpFoundation\Response
27
     */
28
    public function indexAction()
29
    {
30
        $cronManager = new CronManager();
31
        $this->addFlash('message', $cronManager->getOutput());
32
        $this->addFlash('error', $cronManager->getError());
33
34
        $form = $this->createForm(new CronType(), new Cron());
35
36
        return $this->render('FOACronBundle:Dashboard:index.html.twig', [
37
            'crons' => $cronManager->get(),
38
            'raw'   => $cronManager->getRaw(),
39
            'form'  => $form->createView(),
40
        ]);
41
    }
42
43
    /**
44
     * Add a cron to the cron table
45
     *
46
     * @param Request $request
47
     *
48
     * @return RedirectResponse|Response
49
     */
50
    public function addAction(Request $request)
51
    {
52
        $cronManager = new CronManager();
53
        $cron = new Cron();
54
        $this->addFlash('message', $cronManager->getOutput());
55
        $this->addFlash('error', $cronManager->getError());
56
        $form = $this->createForm(new CronType(), $cron);
57
58
        $form->handleRequest($request);
59 View Code Duplication
        if ($form->isValid()) {
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...
60
            $cronManager->add($cron);
61
            $this->addFlash('message', $cronManager->getOutput());
62
            $this->addFlash('error', $cronManager->getError());
63
64
            return $this->redirect($this->generateUrl('foa_cron_index'));
65
        }
66
67
        return $this->render('FOACronBundle:Dashboard:index.html.twig', [
68
            'crons' => $cronManager->get(),
69
            'raw'   => $cronManager->getRaw(),
70
            'form'  => $form->createView(),
71
        ]);
72
    }
73
74
    /**
75
     * Edit a cron
76
     *
77
     * @param $id - the line of the cron in the cron table
78
     *
79
     * @return RedirectResponse|Response
80
     */
81
    public function editAction($id)
82
    {
83
        $cronManager = new CronManager();
84
        $cronList = $cronManager->get();
85
        $this->addFlash('message', $cronManager->getOutput());
86
        $this->addFlash('error', $cronManager->getError());
87
        $form = $this->createForm(new CronType(), $cronList[$id]);
88
89
        $request = $this->get('request');
90
        $form->handleRequest($request);
91 View Code Duplication
        if ($form->isValid()) {
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
            $cronManager->write();
93
94
            $this->addFlash('message', $cronManager->getOutput());
95
            $this->addFlash('error', $cronManager->getError());
96
97
            return $this->redirect($this->generateUrl('foa_cron_index'));
98
        }
99
100
        return $this->render('FOACronBundle:Dashboard:edit.html.twig', [
101
            'id'   => $id,
102
            'form' => $form->createView(),
103
        ]);
104
    }
105
106
    /**
107
     * Wake up a cron from the cron table
108
     *
109
     * @param $id - the line of the cron in the cron table
110
     *
111
     * @return RedirectResponse
112
     */
113
    public function wakeupAction($id)
114
    {
115
        $this->suspendTask($id, false);
116
117
        return $this->redirect($this->generateUrl('foa_cron_index'));
118
    }
119
120
    /**
121
     * Suspend a cron from the cron table
122
     *
123
     * @param $id - the line of the cron in the cron table
124
     *
125
     * @return RedirectResponse
126
     */
127
    public function suspendAction($id)
128
    {
129
        $this->suspendTask($id, true);
130
131
        return $this->redirect($this->generateUrl('foa_cron_index'));
132
    }
133
134
    /**
135
     * Suspend a task from the cron table
136
     *
137
     * @param int $id - the line of the cron in the cron table
138
     * @param bool $state
139
     */
140
    protected function suspendTask($id, $state)
141
    {
142
        $cronManager = new CronManager();
143
        $cronList = $cronManager->get();
144
        $this->addFlash('message', $cronManager->getOutput());
145
        $this->addFlash('error', $cronManager->getError());
146
147
        $cron = $cronList[$id];
148
        $cron->setSuspended($state);
149
150
        $cronManager->write();
151
        $this->addFlash('message', $cronManager->getOutput());
152
        $this->addFlash('error', $cronManager->getError());
153
    }
154
155
    /**
156
     * Remove a cron from the cron table
157
     *
158
     * @param $id - the line of the cron in the cron table
159
     *
160
     * @return RedirectResponse
161
     */
162
    public function removeAction($id)
163
    {
164
        $cronManager = new CronManager();
165
        $this->addFlash('message', $cronManager->getOutput());
166
        $this->addFlash('error', $cronManager->getError());
167
        $cronManager->remove($id);
168
        $this->addFlash('message', $cronManager->getOutput());
169
        $this->addFlash('error', $cronManager->getError());
170
171
        return $this->redirect($this->generateUrl('foa_cron_index'));
172
    }
173
174
    /**
175
     * Gets a log file
176
     *
177
     * @param $id   - the line of the cron in the cron table
178
     * @param $type - the type of file, log or error
179
     *
180
     * @return Response
181
     */
182
    public function fileAction($id, $type)
183
    {
184
        $cronManager = new CronManager();
185
        $cronList = $cronManager->get();
186
187
        /**
188
         * @var Cron $cron
189
         */
190
        $cron = $cronList[$id];
191
192
        $data = [];
193
        $data['file'] = ($type == 'log') ? $cron->getLogFile() : $cron->getErrorFile();
194
        $data['content'] = file_get_contents($data['file']);
195
196
        $serializer = new Serializer([], ['json' => new JsonEncoder()]);
197
198
        return new Response($serializer->serialize($data, 'json'));
199
    }
200
201
    /**
202
     * Adds a flash to the flash bag where flashes are array of messages
203
     *
204
     * @param $type
205
     * @param $message
206
     */
207
    protected function addFlash($type, $message)
208
    {
209
        if (empty($message)) {
210
            return;
211
        }
212
213
        $session = $this->get('session');
214
        $session->getFlashBag()->add($type, $message);
215
    }
216
}
217