|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\os2forms_fordelingskomponent\Drush\Commands; |
| 6 | + |
| 7 | +use ItkDev\Serviceplatformen\Service\SF2900\SF2900\SftpHelper; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 13 | + |
| 14 | +// phpcs:disable Drupal.Commenting.ClassComment.Missing |
| 15 | +#[AsCommand( |
| 16 | + name: 'os2forms-fordelingskomponent:sftp:put', |
| 17 | + description: 'Put file on SFTP server', |
| 18 | +)] |
| 19 | +final class SftpPutCommand extends AbstractCommand { |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritdoc} |
| 23 | + * |
| 24 | + * @see https://www.drush.org/13.x/commands/ |
| 25 | + */ |
| 26 | + protected function configure(): void { |
| 27 | + $this |
| 28 | + ->addArgument('filename', InputArgument::REQUIRED, 'Name of file to put') |
| 29 | + ->addArgument('dir', InputArgument::OPTIONAL, 'Target directory', [SftpHelper::OUTGOING_FOLDER]); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritdoc} |
| 34 | + */ |
| 35 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 36 | + $io = new SymfonyStyle($input, $output); |
| 37 | + $sftp = $this->helper->sf2900()->sftp(); |
| 38 | + |
| 39 | + $filename = $input->getArgument('filename'); |
| 40 | + |
| 41 | + try { |
| 42 | + $result = $sftp->putFile($filename); |
| 43 | + $io->success(sprintf('File %s put on SFTP server as %s', $filename, $result)); |
| 44 | + |
| 45 | + return self::SUCCESS; |
| 46 | + } |
| 47 | + catch (\Exception $exception) { |
| 48 | + $io->error($exception->getMessage()); |
| 49 | + |
| 50 | + return self::FAILURE; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments