| Total Complexity | 42 |
| Total Lines | 339 |
| Duplicated Lines | 11.5 % |
| Coverage | 0% |
| Changes | 0 | ||
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:
Complex classes like QueueController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueueController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class QueueController extends Controller |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Summary stats. |
||
| 21 | * |
||
| 22 | * @Route("/") |
||
| 23 | * @Route("/status/") |
||
| 24 | * @Template("@DtcQueue/Queue/jobs.html.twig") |
||
| 25 | */ |
||
| 26 | public function statusAction() |
||
| 27 | { |
||
| 28 | $params = array(); |
||
| 29 | $jobManager = $this->get('dtc_queue.job_manager'); |
||
| 30 | |||
| 31 | $params['status'] = $jobManager->getStatus(); |
||
| 32 | $this->addCssJs($params); |
||
| 33 | |||
| 34 | return $params; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List jobs in system by default. |
||
| 39 | * |
||
| 40 | * @Route("/jobs_all", name="dtc_queue_jobs_all") |
||
| 41 | */ |
||
| 42 | View Code Duplication | public function jobsAllAction() |
|
| 43 | { |
||
| 44 | $this->validateManagerType('dtc_queue.default_manager'); |
||
| 45 | $class1 = $this->container->getParameter('dtc_queue.class_job'); |
||
| 46 | $class2 = $this->container->getParameter('dtc_queue.class_job_archive'); |
||
| 47 | $label1 = 'Non-Archived Jobs'; |
||
| 48 | $label2 = 'Archived Jobs'; |
||
| 49 | |||
| 50 | $params = $this->getDualGridParams($class1, $class2, $label1, $label2); |
||
| 51 | |||
| 52 | return $this->render('@DtcQueue/Queue/grid.html.twig', $params); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * List jobs in system by default. |
||
| 57 | * |
||
| 58 | * @Template("@DtcQueue/Queue/grid.html.twig") |
||
| 59 | * @Route("/jobs", name="dtc_queue_jobs") |
||
| 60 | */ |
||
| 61 | public function jobsAction() |
||
| 62 | { |
||
| 63 | $this->validateManagerType('dtc_queue.default_manager'); |
||
| 64 | $managerType = $this->container->getParameter('dtc_queue.default_manager'); |
||
| 65 | $rendererFactory = $this->get('dtc_grid.renderer.factory'); |
||
| 66 | $renderer = $rendererFactory->create('datatables'); |
||
| 67 | $gridSource = $this->get('dtc_queue.grid_source.live_jobs.'.('mongodb' === $managerType ? 'odm' : $managerType)); |
||
| 68 | $renderer->bind($gridSource); |
||
| 69 | $params = $renderer->getParams(); |
||
| 70 | $this->addCssJs($params); |
||
| 71 | |||
| 72 | return $params; |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function validateManagerType($type) |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $class1 |
||
| 85 | * @param string $class2 |
||
| 86 | * @param string $label1 |
||
| 87 | * @param string $label2 |
||
| 88 | * |
||
| 89 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 90 | * |
||
| 91 | * @throws \Exception |
||
| 92 | */ |
||
| 93 | protected function getDualGridParams($class1, $class2, $label1, $label2) |
||
| 94 | { |
||
| 95 | $rendererFactory = $this->get('dtc_grid.renderer.factory'); |
||
| 96 | $renderer = $rendererFactory->create('datatables'); |
||
| 97 | $gridSource = $this->get('dtc_grid.manager.source')->get($class1); |
||
| 98 | $renderer->bind($gridSource); |
||
| 99 | $params = $renderer->getParams(); |
||
| 100 | |||
| 101 | $renderer2 = $rendererFactory->create('datatables'); |
||
| 102 | $gridSource = $this->get('dtc_grid.manager.source')->get($class2); |
||
| 103 | $renderer2->bind($gridSource); |
||
| 104 | $params2 = $renderer2->getParams(); |
||
| 105 | |||
| 106 | $params['archive_grid'] = $params2['dtc_grid']; |
||
| 107 | |||
| 108 | $params['dtc_queue_grid_label1'] = $label1; |
||
| 109 | $params['dtc_queue_grid_label2'] = $label2; |
||
| 110 | $this->addCssJs($params); |
||
| 111 | |||
| 112 | return $params; |
||
| 113 | } |
||
| 114 | |||
| 115 | protected function addCssJs(array &$params) |
||
| 122 | } |
||
| 123 | |||
| 124 | protected function validateRunManager() |
||
| 125 | { |
||
| 126 | if ($this->container->hasParameter('dtc_queue.run_manager')) { |
||
| 127 | $this->validateManagerType('dtc_queue.run_manager'); |
||
| 128 | } else { |
||
| 129 | $this->validateManagerType('dtc_queue.default_manager'); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * List jobs in system by default. |
||
| 135 | * |
||
| 136 | * @Route("/runs", name="dtc_queue_runs") |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function runsAction() |
|
| 139 | { |
||
| 140 | $this->validateRunManager(); |
||
| 141 | $class1 = $this->container->getParameter('dtc_queue.class_run'); |
||
| 142 | $class2 = $this->container->getParameter('dtc_queue.class_run_archive'); |
||
| 143 | $label1 = 'Live Runs'; |
||
| 144 | $label2 = 'Archived Runs'; |
||
| 145 | |||
| 146 | $params = $this->getDualGridParams($class1, $class2, $label1, $label2); |
||
| 147 | |||
| 148 | return $this->render('@DtcQueue/Queue/grid.html.twig', $params); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * List registered workers in the system. |
||
| 153 | * |
||
| 154 | * @Route("/workers", name="dtc_queue_workers") |
||
| 155 | * @Template("@DtcQueue/Queue/workers.html.twig") |
||
| 156 | */ |
||
| 157 | public function workersAction() |
||
| 158 | { |
||
| 159 | $workerManager = $this->get('dtc_queue.worker_manager'); |
||
| 160 | $workers = $workerManager->getWorkers(); |
||
| 161 | |||
| 162 | $workerList = []; |
||
| 163 | foreach ($workers as $workerName => $worker) { |
||
| 164 | /* @var Worker $worker */ |
||
| 165 | $workerList[$workerName] = get_class($worker); |
||
| 166 | } |
||
| 167 | $params = ['workers' => $workerList]; |
||
| 168 | $this->addCssJs($params); |
||
| 169 | |||
| 170 | return $params; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Show a graph of job trends. |
||
| 175 | * |
||
| 176 | * @Route("/trends", name="dtc_queue_trends") |
||
| 177 | * @Template("@DtcQueue/Queue/trends.html.twig") |
||
| 178 | */ |
||
| 179 | public function trendsAction() |
||
| 180 | { |
||
| 181 | $recordTimings = $this->container->getParameter('dtc_queue.record_timings'); |
||
| 182 | $params = ['record_timings' => $recordTimings]; |
||
| 183 | $this->addCssJs($params); |
||
| 184 | |||
| 185 | return $params; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @Route("/timings", name="dtc_queue_timings") |
||
| 190 | * |
||
| 191 | * @param Request $request |
||
| 192 | */ |
||
| 193 | public function getTimingsAction() |
||
| 194 | { |
||
| 195 | $request = $this->get('request_stack')->getMasterRequest(); |
||
| 196 | $begin = $request->query->get('begin'); |
||
| 197 | $end = $request->query->get('end'); |
||
| 198 | $type = $request->query->get('type', 'HOUR'); |
||
| 199 | $beginDate = \DateTime::createFromFormat(DATE_ISO8601, $begin) ?: null; |
||
| 200 | $endDate = \DateTime::createFromFormat(DATE_ISO8601, $end) ?: new \DateTime(); |
||
| 201 | |||
| 202 | $recordTimings = $this->container->getParameter('dtc_queue.record_timings'); |
||
| 203 | $params = []; |
||
| 204 | if ($recordTimings) { |
||
| 205 | $params = $this->calculateTimings($type, $beginDate, $endDate); |
||
| 206 | } |
||
| 207 | return new JsonResponse($params); |
||
| 208 | } |
||
| 209 | |||
| 210 | protected function calculateTimings($type, $beginDate, $endDate) { |
||
| 211 | $params = []; |
||
| 212 | $this->validateRunManager(); |
||
| 213 | |||
| 214 | /** @var BaseRunManager $runManager */ |
||
| 215 | $runManager = $this->get('dtc_queue.run_manager'); |
||
| 216 | if ($runManager instanceof RunManager) { |
||
| 217 | $timings = $this->getJobTimingsOdm($type, $endDate, $beginDate); |
||
| 218 | } else { |
||
| 219 | $timings = $this->getJobTimingsOrm($type, $endDate, $beginDate); |
||
| 220 | } |
||
| 221 | uksort($timings, function ($date1str, $date2str) { |
||
| 222 | $date1 = \DateTime::createFromFormat('Y-m-d H', $date1str); |
||
| 223 | $date2 = \DateTime::createFromFormat('Y-m-d H', $date2str); |
||
| 224 | if (!$date2) { |
||
| 225 | return false; |
||
| 226 | } |
||
| 227 | if (!$date1) { |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | return $date1 > $date2; |
||
| 232 | }); |
||
| 233 | |||
| 234 | $params['timings_dates'] = array_keys($timings); |
||
| 235 | $params['timings_data'] = array_values($timings); |
||
| 236 | return $params; |
||
| 237 | } |
||
| 238 | |||
| 239 | protected function getJobTimingsOdm($type, \DateTime $end, \DateTime $begin = null) |
||
| 240 | { |
||
| 241 | /** @var RunManager $runManager */ |
||
| 242 | $runManager = $this->get('dtc_queue.run_manager'); |
||
| 243 | $jobTimingClass = $runManager->getJobTimingClass(); |
||
| 244 | /** @var DocumentManager $documentManager */ |
||
| 245 | $documentManager = $runManager->getObjectManager(); |
||
| 246 | |||
| 247 | $regexInfo = $this->getRegexDate($type); |
||
| 248 | if (!$begin) { |
||
| 249 | $begin = clone $end; |
||
| 250 | $begin->sub($regexInfo['interval']); |
||
| 251 | } |
||
| 252 | |||
| 253 | // Run a map reduce function get worker and status break down |
||
| 254 | $mapFunc = "function() { |
||
| 255 | var dateStr = this.finishedAt.toISOString(); |
||
| 256 | dateStr = dateStr.replace(/{$regexInfo['regex']}/,'{$regexInfo['replacement']}'); |
||
| 257 | var dateBegin = new Date('{$begin->format('c')}'); |
||
| 258 | var dateEnd = new Date('{$end->format('c')}'); |
||
| 259 | if (this.finishedAt >= dateBegin && this.finishedAt <= dateEnd) { |
||
| 260 | emit(dateStr, 1); |
||
| 261 | } |
||
| 262 | }"; |
||
| 263 | $reduceFunc = 'function(k, vals) { |
||
| 264 | return Array.sum(vals); |
||
| 265 | }'; |
||
| 266 | |||
| 267 | $builder = $documentManager->createQueryBuilder($jobTimingClass); |
||
| 268 | $builder->map($mapFunc) |
||
| 269 | ->reduce($reduceFunc); |
||
| 270 | $query = $builder->getQuery(); |
||
| 271 | $results = $query->execute(); |
||
| 272 | $resultHash = []; |
||
| 273 | foreach ($results as $info) { |
||
| 274 | $resultHash[$info['_id']] = $info['value']; |
||
| 275 | } |
||
| 276 | |||
| 277 | return $resultHash; |
||
| 278 | } |
||
| 279 | |||
| 280 | protected function getRegexDate($type) |
||
| 300 | } |
||
| 301 | |||
| 302 | protected function getOrmGroupBy($type) |
||
| 322 | } |
||
| 323 | |||
| 324 | protected function getJobTimingsOrm($type, \DateTime $end, \DateTime $begin = null) |
||
| 356 | } |
||
| 357 | } |
||
| 358 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths