Completed
Push — master ( 4317fb...b1ebb8 )
by Matthew
04:36 queued 02:27
created

QueueController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 181
Duplicated Lines 13.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 24
loc 181
ccs 0
cts 102
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A statusAction() 0 10 1
A jobsAllAction() 12 12 1
A archiveAction() 0 22 2
A jobsAction() 0 15 2
A runningJobsAction() 0 13 2
A getDualGridParams() 0 21 1
A runsAction() 12 12 1
A workersAction() 0 15 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 Dtc\QueueBundle\Controller;
4
5
use Dtc\QueueBundle\Model\Worker;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\StreamedResponse;
12
13
class QueueController extends Controller
14
{
15
    use ControllerTrait;
16
17
    /**
18
     * Summary stats.
19
     *
20
     * @Route("/")
21
     * @Route("/status/")
22
     * @Template("@DtcQueue/Queue/status.html.twig")
23
     */
24
    public function statusAction()
25
    {
26
        $params = array();
27
        $jobManager = $this->get('dtc_queue.job_manager');
28
29
        $params['status'] = $jobManager->getStatus();
30
        $this->addCssJs($params);
31
32
        return $params;
33
    }
34
35
    /**
36
     * List jobs in system by default.
37
     *
38
     * @Route("/jobs_all", name="dtc_queue_jobs_all")
39
     */
40 View Code Duplication
    public function jobsAllAction()
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...
41
    {
42
        $this->validateManagerType('dtc_queue.default_manager');
43
        $class1 = $this->container->getParameter('dtc_queue.class_job');
44
        $class2 = $this->container->getParameter('dtc_queue.class_job_archive');
45
        $label1 = 'Non-Archived Jobs';
46
        $label2 = 'Archived Jobs';
47
48
        $params = $this->getDualGridParams($class1, $class2, $label1, $label2);
49
50
        return $this->render('@DtcQueue/Queue/grid.html.twig', $params);
51
    }
52
53
    /**
54
     * @Route("/archive", name="dtc_queue_archive")
55
     * @Method({"POST"})
56
     */
57
    public function archiveAction(Request $request)
58
    {
59
        $workerName = $request->get('workerName');
60
        $methodName = $request->get('method');
61
62
        $jobManager = $this->get('dtc_queue.job_manager');
63
        $callback = function ($count) {
64
            echo json_encode(['count' => $count]);
65
            echo "\n";
66
            flush();
67
        };
68
69
        return new StreamedResponse(function () use ($jobManager, $callback, $workerName, $methodName) {
70
            $total = $jobManager->countLiveJobs($workerName, $methodName);
71
            echo json_encode(['total' => $total]);
72
            echo "\n";
73
            flush();
74
            if ($total > 0) {
75
                $jobManager->archiveAllJobs($workerName, $methodName, $callback);
76
            }
77
        });
78
    }
79
80
    /**
81
     * List jobs in system by default.
82
     *
83
     * @Template("@DtcQueue/Queue/jobs.html.twig")
84
     * @Route("/jobs", name="dtc_queue_jobs")
85
     */
86
    public function jobsAction()
87
    {
88
        $this->validateManagerType('dtc_queue.default_manager');
89
        $managerType = $this->container->getParameter('dtc_queue.default_manager');
90
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
91
        $renderer = $rendererFactory->create('datatables');
92
        $gridSource = $this->get('dtc_queue.grid_source.jobs_waiting.'.('mongodb' === $managerType ? 'odm' : $managerType));
93
        $renderer->bind($gridSource);
94
        $params = $renderer->getParams();
95
        $this->addCssJs($params);
96
97
        $params['worker_methods'] = $this->get('dtc_queue.job_manager')->getWorkersAndMethods();
98
99
        return $params;
100
    }
101
102
    /**
103
     * List jobs in system by default.
104
     *
105
     * @Template("@DtcQueue/Queue/jobs_running.html.twig")
106
     * @Route("/jobs_running", name="dtc_queue_jobs_running")
107
     */
108
    public function runningJobsAction()
109
    {
110
        $this->validateManagerType('dtc_queue.default_manager');
111
        $managerType = $this->container->getParameter('dtc_queue.default_manager');
112
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
113
        $renderer = $rendererFactory->create('datatables');
114
        $gridSource = $this->get('dtc_queue.grid_source.jobs_running.'.('mongodb' === $managerType ? 'odm' : $managerType));
115
        $renderer->bind($gridSource);
116
        $params = $renderer->getParams();
117
        $this->addCssJs($params);
118
119
        return $params;
120
    }
121
122
    /**
123
     * @param string $class1
124
     * @param string $class2
125
     * @param string $label1
126
     * @param string $label2
127
     *
128
     * @return \Symfony\Component\HttpFoundation\Response
129
     *
130
     * @throws \Exception
131
     */
132
    protected function getDualGridParams($class1, $class2, $label1, $label2)
133
    {
134
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
135
        $renderer = $rendererFactory->create('datatables');
136
        $gridSource = $this->get('dtc_grid.manager.source')->get($class1);
137
        $renderer->bind($gridSource);
138
        $params = $renderer->getParams();
139
140
        $renderer2 = $rendererFactory->create('datatables');
141
        $gridSource = $this->get('dtc_grid.manager.source')->get($class2);
142
        $renderer2->bind($gridSource);
143
        $params2 = $renderer2->getParams();
144
145
        $params['archive_grid'] = $params2['dtc_grid'];
146
147
        $params['dtc_queue_grid_label1'] = $label1;
148
        $params['dtc_queue_grid_label2'] = $label2;
149
        $this->addCssJs($params);
150
151
        return $params;
152
    }
153
154
    /**
155
     * List jobs in system by default.
156
     *
157
     * @Route("/runs", name="dtc_queue_runs")
158
     */
159 View Code Duplication
    public function runsAction()
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...
160
    {
161
        $this->validateRunManager();
162
        $class1 = $this->container->getParameter('dtc_queue.class_run');
163
        $class2 = $this->container->getParameter('dtc_queue.class_run_archive');
164
        $label1 = 'Live Runs';
165
        $label2 = 'Archived Runs';
166
167
        $params = $this->getDualGridParams($class1, $class2, $label1, $label2);
168
169
        return $this->render('@DtcQueue/Queue/grid.html.twig', $params);
170
    }
171
172
    /**
173
     * List registered workers in the system.
174
     *
175
     * @Route("/workers", name="dtc_queue_workers")
176
     * @Template("@DtcQueue/Queue/workers.html.twig")
177
     */
178
    public function workersAction()
179
    {
180
        $workerManager = $this->get('dtc_queue.worker_manager');
181
        $workers = $workerManager->getWorkers();
182
183
        $workerList = [];
184
        foreach ($workers as $workerName => $worker) {
185
            /* @var Worker $worker */
186
            $workerList[$workerName] = get_class($worker);
187
        }
188
        $params = ['workers' => $workerList];
189
        $this->addCssJs($params);
190
191
        return $params;
192
    }
193
}
194