|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OCA\ProfileFields\Command\Data; |
| 11 | + |
| 12 | +use JsonException; |
| 13 | +use OCA\ProfileFields\Service\DataImportService; |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class Import extends Command { |
| 20 | + public function __construct( |
| 21 | + private DataImportService $dataImportService, |
| 22 | + ) { |
| 23 | + parent::__construct(); |
| 24 | + } |
| 25 | + |
| 26 | + #[\Override] |
| 27 | + protected function configure(): void { |
| 28 | + $this |
| 29 | + ->setName('profile_fields:data:import') |
| 30 | + ->setDescription('Import persisted Profile Fields definitions and values from a JSON payload') |
| 31 | + ->addOption( |
| 32 | + name: 'input', |
| 33 | + shortcut: 'i', |
| 34 | + mode: InputOption::VALUE_REQUIRED, |
| 35 | + description: 'Read the JSON import payload from a file', |
| 36 | + ) |
| 37 | + ->addOption( |
| 38 | + name: 'dry-run', |
| 39 | + shortcut: null, |
| 40 | + mode: InputOption::VALUE_NONE, |
| 41 | + description: 'Validate the payload and report the import summary without persisting data', |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + #[\Override] |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 47 | + $sourcePath = $input->getOption('input'); |
| 48 | + if (!is_string($sourcePath) || $sourcePath === '') { |
| 49 | + $output->writeln('<error>Please provide --input with a JSON file path.</error>'); |
| 50 | + return self::FAILURE; |
| 51 | + } |
| 52 | + |
| 53 | + $rawPayload = @file_get_contents($sourcePath); |
| 54 | + if ($rawPayload === false) { |
| 55 | + $output->writeln(sprintf('<error>Could not read import payload from %s.</error>', $sourcePath)); |
| 56 | + return self::FAILURE; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + $decodedPayload = json_decode($rawPayload, true, 512, JSON_THROW_ON_ERROR); |
| 61 | + } catch (JsonException $exception) { |
| 62 | + $output->writeln('<error>Failed to decode import payload JSON.</error>'); |
| 63 | + $output->writeln(sprintf('<error>%s</error>', $exception->getMessage())); |
| 64 | + return self::FAILURE; |
| 65 | + } |
| 66 | + |
| 67 | + if (!is_array($decodedPayload)) { |
| 68 | + $output->writeln('<error>Import payload must decode to a JSON object.</error>'); |
| 69 | + return self::FAILURE; |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + $summary = $this->dataImportService->import($decodedPayload, (bool)$input->getOption('dry-run')); |
| 74 | + } catch (\Throwable $throwable) { |
| 75 | + $output->writeln('<error>Import validation failed.</error>'); |
| 76 | + $output->writeln(sprintf('<error>%s</error>', $throwable->getMessage())); |
| 77 | + return self::FAILURE; |
| 78 | + } |
| 79 | + |
| 80 | + $output->writeln((bool)$input->getOption('dry-run') |
| 81 | + ? '<info>Profile Fields data import dry-run completed.</info>' |
| 82 | + : '<info>Profile Fields data imported.</info>'); |
| 83 | + $output->writeln(sprintf( |
| 84 | + '<info>Definitions: %d created, %d updated, %d skipped.</info>', |
| 85 | + $summary['created_definitions'], |
| 86 | + $summary['updated_definitions'], |
| 87 | + $summary['skipped_definitions'], |
| 88 | + )); |
| 89 | + $output->writeln(sprintf( |
| 90 | + '<info>Values: %d created, %d updated, %d skipped.</info>', |
| 91 | + $summary['created_values'], |
| 92 | + $summary['updated_values'], |
| 93 | + $summary['skipped_values'], |
| 94 | + )); |
| 95 | + |
| 96 | + return self::SUCCESS; |
| 97 | + } |
| 98 | +} |
0 commit comments