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

CountCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
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