Completed
Pull Request — master (#40)
by Matthew
17:20
created

CountCommand::execute()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0796

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 15
cts 17
cp 0.8824
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 3
nop 2
crap 7.0796
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
    protected function configure()
12 4
    {
13
        $this
14 4
            ->setName('dtc:queue:count')
15 4
            ->setDescription('Display job queue status.');
16 4
    }
17 4
18
    protected function execute(InputInterface $input, OutputInterface $output)
19 4
    {
20
        $container = $this->getContainer();
21 4
        $jobManager = $container->get('dtc_queue.manager.job');
22 4
23
        $waitingCount = $jobManager->getWaitingJobCount();
24 4
        $status = $jobManager->getStatus();
25 1
26
        $firstJob = key($status);
27 1
        if ($firstJob) {
28
            $jobKeys = array_keys($status);
29
            $maxLength = max(array_map(function ($item) {
30 3
                return strlen($item ?: '');
31
            }, $jobKeys));
32 3
            $formatLen = $maxLength > 50 ? 50 : $maxLength;
33 3
            $format = '%-'.$formatLen.'s';
34 3
            $headingArgs = ['Job name'];
35 3
            $initialKeys = array_keys($status[$firstJob]);
36
            foreach ($initialKeys as $statusName) {
37 3
                $headingStr = ucwords(str_replace('_', ' ', $statusName));
38
                $format .= ' %'.(1 + strlen($headingStr)).'s';
39
                $headingArgs[] = $headingStr;
40 2
            }
41
            array_unshift($headingArgs, $format);
42 2
            $msg = call_user_func_array('sprintf', $headingArgs);
43 2
            $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