Completed
Push — develop ( edef75...20c46c )
by Novikov
03:25
created

DashboardController::addAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 7
Ratio 30.43 %

Importance

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