BaseCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 82
ccs 0
cts 36
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A printMergeRequestApproval() 0 44 3
A getColor() 0 11 3
A addApproverListTableRow() 0 6 2
1
<?php declare(strict_types=1);
2
3
namespace DanielPieper\MergeReminder\Command;
4
5
use DanielPieper\MergeReminder\ValueObject\MergeRequestApproval;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class BaseCommand extends Command
11
{
12
    /**
13
     * @param OutputInterface $output
14
     * @param MergeRequestApproval $mergeRequestApproval
15
     */
16
    protected function printMergeRequestApproval(
17
        OutputInterface $output,
18
        MergeRequestApproval $mergeRequestApproval
19
    ): void {
20
        $mergeRequest = $mergeRequestApproval->getMergeRequest();
21
        $output->writeln($mergeRequest->getAuthor()->getUsername());
22
        $output->writeln(sprintf(
23
            '[%s] <fg=%s>%s</>',
24
            $mergeRequest->getProject()->getName(),
25
            $this->getColor($mergeRequestApproval),
26
            $mergeRequest->getTitle()
27
        ));
28
        $output->writeln($mergeRequest->getWebUrl());
29
        if ($output->isVerbose()) {
30
            $output->writeln($mergeRequest->getDescription());
31
        }
32
33
        $rows = [
34
            [
35
                'Created:',
36
                $mergeRequestApproval->getCreatedAt()->shortRelativeToNowDiffForHumans()
37
            ],
38
        ];
39
40
        if ($mergeRequestApproval->getUpdatedAt()->diffInDays($mergeRequestApproval->getCreatedAt()) > 0) {
41
            $rows[] = [
42
                'Updated:',
43
                $mergeRequestApproval->getUpdatedAt()->shortRelativeToNowDiffForHumans(),
44
            ];
45
        }
46
        $this->addApproverListTableRow($rows, 'Approvers:', $mergeRequestApproval->getApproverNames());
47
        $this->addApproverListTableRow($rows, 'Approver groups:', $mergeRequestApproval->getApproverGroupNames());
48
        $this->addApproverListTableRow(
49
            $rows,
50
            'Suggested approvers:',
51
            $mergeRequestApproval->getSuggestedApproverNames()
52
        );
53
54
        $table = new Table($output);
55
        $table->setStyle('compact');
56
        $table->setRows($rows);
57
        $table->render();
58
59
        $output->writeln('');
60
    }
61
62
    /**
63
     * @param array $rows
64
     * @param $title
65
     * @param array $approverNames
66
     */
67
    private function addApproverListTableRow(array &$rows, $title, array $approverNames): void
68
    {
69
        if (count($approverNames) > 0) {
70
            array_push($rows, [
71
                $title,
72
                implode(', ', $approverNames),
73
            ]);
74
        }
75
    }
76
77
    /**
78
     * @param MergeRequestApproval $mergeRequestApproval
79
     * @return string
80
     */
81
    private function getColor(MergeRequestApproval $mergeRequestApproval): string
82
    {
83
        $ageInDays = $mergeRequestApproval->getCreatedAt()->diffInDays();
84
85
        if ($ageInDays > 2) {
86
            return 'red';
87
        }
88
        if ($ageInDays > 1) {
89
            return 'yellow';
90
        }
91
        return 'green';
92
    }
93
}
94