CommandTrait::formatTaskBlock()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
ccs 19
cts 19
cp 1
cc 5
eloc 20
nc 6
nop 2
crap 5
1
<?php
2
3
namespace JobQueue\Application\Utils;
4
5
use JobQueue\Domain\Task\Task;
6
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
7
use Symfony\Component\Console\Helper\HelperInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
trait CommandTrait
11
{
12
    /**
13
     *
14
     * @param OutputInterface $output
15
     */
16 3
    private function setStyles(OutputInterface $output): void
17
    {
18
        $styles = [
19 3
            'waiting'  => new OutputFormatterStyle('blue'),
20 3
            'running'  => new OutputFormatterStyle('cyan', null, ['bold', 'blink']),
21 3
            'finished' => new OutputFormatterStyle('green'),
22 3
            'failed'   => new OutputFormatterStyle('red', null, ['bold']),
23
        ];
24
25 3
        foreach ($styles as $name => $style) {
26
            $output
27 3
                ->getFormatter()
28 3
                ->setStyle($name, $style)
29
            ;
30
        }
31 3
    }
32
33
    /**
34
     *
35
     * @param string          $text
36
     * @param string          $type
37
     * @param OutputInterface $output
38
     */
39 4
    private function formatSection(string $text, string $type, OutputInterface $output): void {
40 4
        $formatter = $this->getHelper('formatter'); /** @var \Symfony\Component\Console\Helper\FormatterHelper $formatter */
41
42 4
        $block = $formatter->formatSection($type, $text, $type);
43
44 4
        $output->writeln($block);
45 4
    }
46
47
    /**
48
     *
49
     * @param string          $text
50
     * @param OutputInterface $output
51
     */
52 4
    private function formatInfoSection(string $text, OutputInterface $output): void {
53 4
        $this->formatSection($text, 'info', $output);
54 4
    }
55
56
    /**
57
     *
58
     * @param string          $text
59
     * @param OutputInterface $output
60
     */
61
    private function formatErrorSection(string $text, OutputInterface $output): void {
62
        $this->formatSection($text, 'error', $output);
63
    }
64
65
    /**
66
     *
67
     * @param string          $text
68
     * @param string          $type
69
     * @param OutputInterface $output
70
     */
71 3
    private function formatBlock(string $text, string $type, OutputInterface $output): void {
72
        /** @var \Symfony\Component\Console\Helper\FormatterHelper $formatter */
73 3
        $formatter = $this->getHelper('formatter');
74
75 3
        $block = $formatter->formatBlock([$type, $text], $type, true);
76
77 3
        $output->writeln($block);
78 3
    }
79
80
    /**
81
     *
82
     * @param string          $text
83
     * @param OutputInterface $output
84
     */
85
    private function formatInfoBlock(string $text, OutputInterface $output): void {
86
        $this->formatBlock($text, 'info', $output);
87
    }
88
89
    /**
90
     *
91
     * @param Task            $task
92
     * @param OutputInterface $output
93
     */
94 3
    private function formatTaskBlock(Task $task, OutputInterface $output): void {
95 3
        $i = 0;
96 3
        $paramString = null;
97 3
        foreach ($task->getParameters() as $name => $value) {
98 3
            if ($i) {
99 3
                $paramString .= "                 ";
100
            }
101 3
            $i++;
102
103 3
            $paramString .= "$i) $name: $value \n";
104
        }
105 3
        $paramString = $paramString ?: '(none)';
106
107 3
        $tagString = $task->getTags() ? implode(', ', $task->getTags()) : '(none)';
108
109
        $text = <<<EOL
110 3
- Identifier : {$task->getIdentifier()}
111 3
  - Date       : {$task->getCreatedAt('r')}
112 3
  - Job class  : {$task->getJobName()} 
113 3
  - Job name   : {$task->getJobName(true)} 
114 3
  - Profile    : {$task->getProfile()} 
115 3
  - Status     : {$task->getStatus()} 
116 3
  - Tags       : $tagString
117 3
  - Parameters : $paramString 
118
EOL;
119
120 3
        $this->formatBlock($text, 'info', $output);
121 3
    }
122
123
    /**
124
     *
125
     * @param string          $value
126
     * @param string          $style
127
     * @param OutputInterface $output
128
     * @return string
129
     */
130 3
    private function formatContent(string $value, string $style, OutputInterface $output): string {
131
        return $output
132 3
            ->getFormatter()
133 3
            ->format(sprintf('<%s>%s</>', $style, $value));
134
    }
135
136
    /**
137
     *
138
     * @return HelperInterface
139
     */
140
    abstract public function getHelper($name);
141
}
142