Completed
Push — master ( a21736...be5db4 )
by Matthew
16:44 queued 07:17
created

CountCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
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 4
        $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 1
            $formatLen = $maxLength > 50 ? 50 : $maxLength;
33 1
            $formatMinLen = strlen('Job name') + 1;
34 1
            $formatLen = $formatLen < $formatMinLen ? $formatMinLen : $formatLen;
35 1
            $format = '%-'.$formatLen.'s';
36 1
            $headingArgs = ['Job name'];
37 1
            $initialKeys = array_keys($status[$firstJob]);
38 1
            foreach ($initialKeys as $statusName) {
39 1
                $headingStr = ucwords(str_replace('_', ' ', $statusName));
40 1
                $format .= ' %'.(1 + strlen($headingStr)).'s';
41 1
                $headingArgs[] = $headingStr;
42 1
            }
43 1
            array_unshift($headingArgs, $format);
44 1
            $msg = call_user_func_array('sprintf', $headingArgs);
45 1
            $output->writeln($msg);
46
47 1
            foreach ($status as $func => $info) {
48 1
                $lineArgs = [$format, $func];
49 1
                foreach ($initialKeys as $statusKey) {
50 1
                    $lineArgs[] = $info[$statusKey];
51 1
                }
52 1
                $msg = call_user_func_array('sprintf', $lineArgs);
53 1
                $output->writeln($msg);
54 1
            }
55 1
        }
56
57 3
        $output->writeln("Total waiting jobs: {$waitingCount}");
58 3
    }
59
}
60