Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Check
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 configure
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
90
 getStatusColor
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Command\Configure;
10
11use OC\Core\Command\Base;
12use OCA\Libresign\Service\SetupCheck\ConfigureCheckResult;
13use OCA\Libresign\Service\SetupCheckResultService;
14use Symfony\Component\Console\Helper\Table;
15use Symfony\Component\Console\Helper\TableCell;
16use Symfony\Component\Console\Helper\TableCellStyle;
17use Symfony\Component\Console\Input\InputInterface;
18use Symfony\Component\Console\Input\InputOption;
19use Symfony\Component\Console\Output\OutputInterface;
20
21class Check extends Base {
22
23    public function __construct(
24        private SetupCheckResultService $setupCheckResultService,
25    ) {
26        parent::__construct();
27    }
28
29    #[\Override]
30    protected function configure(): void {
31        $this
32            ->setName('libresign:configure:check')
33            ->setDescription('Check configure')
34            ->addOption(
35                name: 'sign',
36                shortcut: 's',
37                mode: InputOption::VALUE_NONE,
38                description: 'Check requirements to sign document'
39            )
40            ->addOption(
41                name: 'certificate',
42                shortcut: 'c',
43                mode: InputOption::VALUE_NONE,
44                description: 'Check requirements to use root certificate'
45            );
46    }
47
48    #[\Override]
49    protected function execute(InputInterface $input, OutputInterface $output): int {
50        $sign = $input->getOption('sign');
51        $certificate = $input->getOption('certificate');
52        $all = (!$sign && !$certificate);
53
54        $allChecks = $this->setupCheckResultService->getFormattedChecks();
55
56        $filteredRows = array_filter($allChecks, function (ConfigureCheckResult $check) use ($all, $sign, $certificate) {
57            if ($all) {
58                return true;
59            }
60            if ($sign && $check->getCategory() === 'system') {
61                return true;
62            }
63            if ($certificate && $check->getCategory() === 'security') {
64                return true;
65            }
66            return false;
67        });
68
69        if (!empty($filteredRows)) {
70            $table = new Table($output);
71            $table->setColumnMaxWidth(3, 40);
72            foreach ($filteredRows as $row) {
73                $table->addRow([
74                    new TableCell($row->getStatus(), ['style' => new TableCellStyle([
75                        'bg' => $this->getStatusColor($row->getStatus()),
76                        'fg' => 'black',
77                        'align' => 'center',
78                    ])]),
79                    $row->getResource(),
80                    $row->getMessage(),
81                    $row->getTip(),
82                ]);
83            }
84            $table
85                ->setHeaders([
86                    'Status',
87                    'Resource',
88                    'Message',
89                    'Tip',
90                ])
91                ->setStyle('symfony-style-guide')
92                ->render();
93        }
94
95        return 0;
96    }
97
98    private function getStatusColor(string $status): string {
99        return match ($status) {
100            'success' => 'green',
101            'error' => 'red',
102            'info' => 'bright-yellow',
103            default => 'red',
104        };
105    }
106}