Completed
Pull Request — master (#40)
by Matthew
12:15 queued 04:06
created

CountCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
C execute() 0 41 8
1
<?php
2
3
namespace Dtc\QueueBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class CountCommand extends ContainerAwareCommand
10
{
11 4
    protected function configure()
12
    {
13
        $this
14 4
            ->setName('dtc:queue:count')
15 4
            ->setDescription('Display job queue status.');
16 4
    }
17
18 4
    protected function execute(InputInterface $input, OutputInterface $output)
19
    {
20 4
        $container = $this->getContainer();
21 4
        $jobManager = $container->get('dtc_queue.manager.job');
22
23 4
        $waitingCount = $jobManager->getWaitingJobCount();
24 4
        $status = $jobManager->getStatus();
25
26 4
        $firstJob = key($status);
27 3
        if ($firstJob) {
28 1
            $jobKeys = array_keys($status);
29 1
            $maxLength = max(array_map(function ($item) {
30 1
                return strlen($item ?: '');
31 1
            }, $jobKeys));
32
            $formatLen = $maxLength > 50 ? 50 : $maxLength;
33
            $formatMinLen = strlen('Job name') + 1;
34
            $formatLen = $formatLen < $formatMinLen ? $formatMinLen : $formatLen;
35
            $format = '%-'.$formatLen.'s';
36
            $headingArgs = ['Job name'];
37
            $initialKeys = array_keys($status[$firstJob]);
38
            foreach ($initialKeys as $statusName) {
39
                $headingStr = ucwords(str_replace('_', ' ', $statusName));
40
                $format .= ' %'.(1 + strlen($headingStr)).'s';
41
                $headingArgs[] = $headingStr;
42
            }
43
            array_unshift($headingArgs, $format);
44
            $msg = call_user_func_array('sprintf', $headingArgs);
45
            $output->writeln($msg);
46
47
            foreach ($status as $func => $info) {
48
                $lineArgs = [$format, $func];
49
                foreach ($initialKeys as $statusKey) {
50
                    $lineArgs[] = $info[$statusKey];
51
                }
52
                $msg = call_user_func_array('sprintf', $lineArgs);
53
                $output->writeln($msg);
54
            }
55
        }
56
57
        $output->writeln("Total waiting jobs: {$waitingCount}");
58
    }
59
}
60