Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.00% covered (warning)
68.00%
17 / 25
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateCommand
68.00% covered (warning)
68.00%
17 / 25
66.67% covered (warning)
66.67%
2 / 3
5.82
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 execute
42.86% covered (danger)
42.86%
6 / 14
0.00% covered (danger)
0.00%
0 / 1
4.68
1<?php
2
3/**
4 * phpMyFAQ Update Console Command
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2025 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2025-06-16
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Command;
21
22use DateTime;
23use phpMyFAQ\Configuration;
24use phpMyFAQ\Setup\UpdateRunner;
25use phpMyFAQ\System;
26use Symfony\Component\Console\Command\Command;
27use Symfony\Component\Console\Input\InputArgument;
28use Symfony\Component\Console\Input\InputInterface;
29use Symfony\Component\Console\Output\OutputInterface;
30use Symfony\Component\Console\Style\SymfonyStyle;
31use Throwable;
32
33class UpdateCommand extends Command
34{
35    protected static string $defaultName = 'phpmyfaq:update';
36
37    private Configuration $configuration;
38
39    private System $system;
40
41    public function __construct()
42    {
43        parent::__construct();
44
45        $this->configuration = Configuration::getConfigurationInstance();
46        $this->system = new System();
47    }
48
49    /**
50     * {@inheritdoc}
51     */
52    protected function configure(): void
53    {
54        $this
55            ->setName(self::$defaultName)
56            ->setDescription(description: 'Executes the phpMyFAQ update process')
57            ->addArgument(
58                name: 'version',
59                mode: InputArgument::OPTIONAL,
60                description: 'Requested version for the update',
61            );
62    }
63
64    protected function execute(InputInterface $input, OutputInterface $output): int
65    {
66        $symfonyStyle = new SymfonyStyle($input, $output);
67
68        $symfonyStyle->title(message: 'Start automatic phpMyFAQ update ...');
69
70        try {
71            $updateRunner = new UpdateRunner($this->configuration, $this->system);
72            $result = $updateRunner->run($symfonyStyle);
73
74            if (Command::SUCCESS !== $result) {
75                return Command::FAILURE;
76            }
77
78            $symfonyStyle->success(message: strtr('phpMyFAQ was successfully updated to version version: on date:.', [
79                'version:' => System::getVersion(),
80                'date:' => new DateTime()->format(format: 'Y-m-d H:i:s'),
81            ]));
82
83            return Command::SUCCESS;
84        } catch (Throwable $throwable) {
85            $symfonyStyle->error(message: 'Error during update: ' . $throwable->getMessage());
86            return Command::FAILURE;
87        }
88    }
89}