Completed
Pull Request — develop (#3)
by jean-marie
01:57
created

DashboardController::fileAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
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
            '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
        $cronList = $cronManager->get();
143
        $this->addFlash('message', $cronManager->getOutput());
144
        $this->addFlash('error', $cronManager->getError());
145
146
        $cron = $cronList[$id];
147
        $cron->setSuspended($state);
148
149
        $cronManager->write();
150
        $this->addFlash('message', $cronManager->getOutput());
151
        $this->addFlash('error', $cronManager->getError());
152
    }
153
154
    /**
155
     * Remove a cron from the cron table
156
     *
157
     * @param $id - the line of the cron in the cron table
158
     *
159
     * @return RedirectResponse
160
     */
161
    public function removeAction($id)
162
    {
163
        $cronManager = new CronManager();
164
        $this->addFlash('message', $cronManager->getOutput());
165
        $this->addFlash('error', $cronManager->getError());
166
        $cronManager->remove($id);
167
        $this->addFlash('message', $cronManager->getOutput());
168
        $this->addFlash('error', $cronManager->getError());
169
170
        return $this->redirect($this->generateUrl('foa_cron_index'));
171
    }
172
173
    /**
174
     * Gets a log file
175
     *
176
     * @param $id   - the line of the cron in the cron table
177
     * @param $type - the type of file, log or error
178
     *
179
     * @return Response
180
     */
181
    public function fileAction($id, $type)
182
    {
183
        $cronManager = new CronManager();
184
        $cronList = $cronManager->get();
185
186
        /**
187
         * @var Cron $cron
188
         */
189
        $cron = $cronList[$id];
190
191
        $data = [];
192
        $data['file'] = ($type == 'log') ? $cron->getLogFile() : $cron->getErrorFile();
193
        $data['content'] = file_get_contents($data['file']);
194
195
        $serializer = new Serializer([], ['json' => new JsonEncoder()]);
196
197
        return new Response($serializer->serialize($data, 'json'));
198
    }
199
200
    /**
201
     * Adds a flash to the flash bag where flashes are array of messages
202
     *
203
     * @param $type
204
     * @param $message
205
     */
206
    protected function addFlash($type, $message)
207
    {
208
        if (empty($message)) {
209
            return;
210
        }
211
212
        $session = $this->get('session');
213
        $session->getFlashBag()->add($type, $message);
214
    }
215
}
216