|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace FactorioItemBrowser\Export\Command; |
| 6 | + |
| 7 | +use BluePsyduck\FactorioModPortalClient\Entity\Version; |
| 8 | +use FactorioItemBrowser\CombinationApi\Client\ClientInterface; |
| 9 | +use FactorioItemBrowser\CombinationApi\Client\Constant\JobPriority; |
| 10 | +use FactorioItemBrowser\CombinationApi\Client\Exception\ClientException; |
| 11 | +use FactorioItemBrowser\CombinationApi\Client\Request\Job\CreateRequest; |
| 12 | +use FactorioItemBrowser\Common\Constant\Constant; |
| 13 | +use FactorioItemBrowser\Common\Constant\Defaults; |
| 14 | +use FactorioItemBrowser\Export\Constant\CommandName; |
| 15 | +use FactorioItemBrowser\Export\Exception\CommandException; |
| 16 | +use FactorioItemBrowser\Export\Exception\ExportException; |
| 17 | +use FactorioItemBrowser\Export\Output\Console; |
| 18 | +use FactorioItemBrowser\Export\Process\CommandProcess; |
| 19 | +use FactorioItemBrowser\Export\Service\FactorioDownloadService; |
| 20 | +use FactorioItemBrowser\Export\Service\ModFileService; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * The command for updating Factorio to its latest version. |
| 27 | + * |
| 28 | + * @author BluePsyduck <[email protected]> |
| 29 | + * @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
| 30 | + */ |
| 31 | +class UpdateFactorioCommand extends Command |
| 32 | +{ |
| 33 | + protected ClientInterface $combinationApiClient; |
| 34 | + protected Console $console; |
| 35 | + protected FactorioDownloadService $factorioDownloadService; |
| 36 | + protected ModFileService $modFileService; |
| 37 | + |
| 38 | + public function __construct( |
| 39 | + ClientInterface $combinationApiClient, |
| 40 | + Console $console, |
| 41 | + FactorioDownloadService $factorioDownloadService, |
| 42 | + ModFileService $modFileService, |
| 43 | + ) { |
| 44 | + parent::__construct(); |
| 45 | + |
| 46 | + $this->combinationApiClient = $combinationApiClient; |
| 47 | + $this->console = $console; |
| 48 | + $this->factorioDownloadService = $factorioDownloadService; |
| 49 | + $this->modFileService = $modFileService; |
| 50 | + } |
| 51 | + |
| 52 | + protected function configure(): void |
| 53 | + { |
| 54 | + parent::configure(); |
| 55 | + |
| 56 | + $this->setName(CommandName::UPDATE_FACTORIO); |
| 57 | + $this->setDescription('Updates the Factorio game to its latest version.'); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param InputInterface $input |
| 62 | + * @param OutputInterface $output |
| 63 | + * @return int |
| 64 | + * @throws ExportException |
| 65 | + */ |
| 66 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 67 | + { |
| 68 | + $currentVersion = $this->modFileService->getInfo(Constant::MOD_NAME_BASE)->version; |
| 69 | + $this->console->writeMessage(sprintf('Current version: <fg=green>%s</>', $currentVersion)); |
| 70 | + |
| 71 | + $latestVersion = $this->factorioDownloadService->getLatestVersion(); |
| 72 | + $this->console->writeMessage(sprintf('Latest version: <fg=green>%s</>', $latestVersion)); |
| 73 | + |
| 74 | + if ((new Version($latestVersion))->compareTo($currentVersion) <= 0) { |
| 75 | + $this->console->writeMessage('Already up-to-date. Done.'); |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + $this->console->writeAction('Downloading new version of Factorio'); |
| 80 | + $process = $this->createDownloadProcess($latestVersion); |
| 81 | + $process->run(function (string $type, string $message) use ($output): void { |
| 82 | + $output->write($message); |
| 83 | + }); |
| 84 | + |
| 85 | + if ($process->getExitCode() !== 0) { |
| 86 | + $this->console->writeMessage('<fg=red>Factorio download failed.</>'); |
| 87 | + return 1; |
| 88 | + } |
| 89 | + |
| 90 | + $this->createExportJob(); |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param string $version |
| 96 | + * @return CommandProcess<string> |
| 97 | + */ |
| 98 | + protected function createDownloadProcess(string $version): CommandProcess |
| 99 | + { |
| 100 | + return new CommandProcess(CommandName::DOWNLOAD_FACTORIO, [$version]); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @throws CommandException |
| 105 | + */ |
| 106 | + protected function createExportJob(): void |
| 107 | + { |
| 108 | + $request = new CreateRequest(); |
| 109 | + $request->combinationId = Defaults::COMBINATION_ID; |
| 110 | + $request->priority = JobPriority::ADMIN; |
| 111 | + |
| 112 | + try { |
| 113 | + $this->combinationApiClient->sendRequest($request); |
| 114 | + } catch (ClientException $e) { |
| 115 | + throw new CommandException('Request to Combination API failed.', 500, $e); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments