Completed
Pull Request — master (#40)
by Matthew
12:28
created

CountCommand::execute()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 3
nop 2
crap 7
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
            $format = '%-'.$formatLen.'s';
34
            $headingArgs = ['Job name'];
35
            $initialKeys = array_keys($status[$firstJob]);
36
            foreach ($initialKeys as $statusName) {
37
                $headingStr = ucwords(str_replace('_', ' ', $statusName));
38
                $format .= ' %'.(1 + strlen($headingStr)).'s';
39
                $headingArgs[] = $headingStr;
40
            }
41
            array_unshift($headingArgs, $format);
42
            $msg = call_user_func_array('sprintf', $headingArgs);
43
            $output->writeln($msg);
44
45
            foreach ($status as $func => $info) {
46
                $lineArgs = [$format, $func];
47
                foreach ($initialKeys as $statusKey) {
48
                    $lineArgs[] = $info[$statusKey];
49
                }
50
                $msg = call_user_func_array('sprintf', $lineArgs);
51
                $output->writeln($msg);
52
            }
53
        }
54
55
        $output->writeln("Total waiting jobs: {$waitingCount}");
56
    }
57
}
58