Completed
Branch feature/redis (23fb44)
by Matthew
05:31
created

QueueController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 184
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 88.37%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 24
loc 184
ccs 76
cts 86
cp 0.8837
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
B archiveAction() 0 25 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 1
    public function statusAction()
25
    {
26 1
        $params = array();
27 1
        $jobManager = $this->get('dtc_queue.manager.job');
28
29 1
        $params['status'] = $jobManager->getStatus();
30 1
        $this->addCssJs($params);
31
32 1
        return $params;
33
    }
34
35
    /**
36
     * List jobs in system by default.
37
     *
38
     * @Route("/jobs_all", name="dtc_queue_jobs_all")
39
     */
40 1 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 1
        $this->validateManagerType('dtc_queue.manager.job');
43 1
        $class1 = $this->container->getParameter('dtc_queue.class.job');
44 1
        $class2 = $this->container->getParameter('dtc_queue.class.job_archive');
45 1
        $label1 = 'Non-Archived Jobs';
46 1
        $label2 = 'Archived Jobs';
47
48 1
        $params = $this->getDualGridParams($class1, $class2, $label1, $label2);
49
50 1
        return $this->render('@DtcQueue/Queue/grid.html.twig', $params);
51
    }
52
53
    /**
54
     * @Route("/archive", name="dtc_queue_archive")
55
     * @Method({"POST"})
56
     */
57 1
    public function archiveAction(Request $request)
58
    {
59 1
        $workerName = $request->get('workerName');
60 1
        $methodName = $request->get('method');
61
62 1
        $jobManager = $this->get('dtc_queue.manager.job');
63 1
        $callback = function ($count) {
64
            echo json_encode(['count' => $count]);
65
            echo "\n";
66
            flush();
67 1
        };
68
69 1
        $streamingResponse = 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 1
        });
78 1
        $streamingResponse->headers->set('Content-Type', 'application/x-ndjson');
79
80 1
        return $streamingResponse;
81
    }
82
83
    /**
84
     * List jobs in system by default.
85
     *
86
     * @Template("@DtcQueue/Queue/jobs.html.twig")
87
     * @Route("/jobs", name="dtc_queue_jobs")
88
     */
89 1
    public function jobsAction()
90
    {
91 1
        $this->validateManagerType('dtc_queue.manager.job');
92 1
        $managerType = $this->container->getParameter('dtc_queue.manager.job');
93 1
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
94 1
        $renderer = $rendererFactory->create('datatables');
95 1
        $gridSource = $this->get('dtc_queue.grid_source.jobs_waiting.'.('mongodb' === $managerType ? 'odm' : $managerType));
96 1
        $renderer->bind($gridSource);
97 1
        $params = $renderer->getParams();
98 1
        $this->addCssJs($params);
99
100 1
        $params['worker_methods'] = $this->get('dtc_queue.manager.job')->getWorkersAndMethods();
101
102 1
        return $params;
103
    }
104
105
    /**
106
     * List jobs in system by default.
107
     *
108
     * @Template("@DtcQueue/Queue/jobs_running.html.twig")
109
     * @Route("/jobs_running", name="dtc_queue_jobs_running")
110
     */
111 1
    public function runningJobsAction()
112
    {
113 1
        $this->validateManagerType('dtc_queue.manager.job');
114 1
        $managerType = $this->container->getParameter('dtc_queue.manager.job');
115 1
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
116 1
        $renderer = $rendererFactory->create('datatables');
117 1
        $gridSource = $this->get('dtc_queue.grid_source.jobs_running.'.('mongodb' === $managerType ? 'odm' : $managerType));
118 1
        $renderer->bind($gridSource);
119 1
        $params = $renderer->getParams();
120 1
        $this->addCssJs($params);
121
122 1
        return $params;
123
    }
124
125
    /**
126
     * @param string $class1
127
     * @param string $class2
128
     * @param string $label1
129
     * @param string $label2
130
     *
131
     * @return \Symfony\Component\HttpFoundation\Response
132
     *
133
     * @throws \Exception
134
     */
135 2
    protected function getDualGridParams($class1, $class2, $label1, $label2)
136
    {
137 2
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
138 2
        $renderer = $rendererFactory->create('datatables');
139 2
        $gridSource = $this->get('dtc_grid.manager.source')->get($class1);
140 2
        $renderer->bind($gridSource);
141 2
        $params = $renderer->getParams();
142
143 2
        $renderer2 = $rendererFactory->create('datatables');
144 2
        $gridSource = $this->get('dtc_grid.manager.source')->get($class2);
145 2
        $renderer2->bind($gridSource);
146 2
        $params2 = $renderer2->getParams();
147
148 2
        $params['archive_grid'] = $params2['dtc_grid'];
149
150 2
        $params['dtc_queue_grid_label1'] = $label1;
151 2
        $params['dtc_queue_grid_label2'] = $label2;
152 2
        $this->addCssJs($params);
153
154 2
        return $params;
155
    }
156
157
    /**
158
     * List jobs in system by default.
159
     *
160
     * @Route("/runs", name="dtc_queue_runs")
161
     */
162 1 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...
163
    {
164 1
        $this->validateRunManager();
165 1
        $class1 = $this->container->getParameter('dtc_queue.class.run');
166 1
        $class2 = $this->container->getParameter('dtc_queue.class.run_archive');
167 1
        $label1 = 'Live Runs';
168 1
        $label2 = 'Archived Runs';
169
170 1
        $params = $this->getDualGridParams($class1, $class2, $label1, $label2);
171
172 1
        return $this->render('@DtcQueue/Queue/grid.html.twig', $params);
173
    }
174
175
    /**
176
     * List registered workers in the system.
177
     *
178
     * @Route("/workers", name="dtc_queue_workers")
179
     * @Template("@DtcQueue/Queue/workers.html.twig")
180
     */
181 1
    public function workersAction()
182
    {
183 1
        $workerManager = $this->get('dtc_queue.manager.worker');
184 1
        $workers = $workerManager->getWorkers();
185
186 1
        $workerList = [];
187 1
        foreach ($workers as $workerName => $worker) {
188
            /* @var Worker $worker */
189
            $workerList[$workerName] = get_class($worker);
190
        }
191 1
        $params = ['workers' => $workerList];
192 1
        $this->addCssJs($params);
193
194 1
        return $params;
195
    }
196
}
197