|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +require 'vendor/autoload.php'; |
| 5 | + |
| 6 | +use Rcsofttech85\FileHandler\DependencyInjection\ServiceContainer; |
| 7 | +use Rcsofttech85\FileHandler\Exception\FileEncryptorException; |
| 8 | +use Rcsofttech85\FileHandler\Exception\FileHandlerException; |
| 9 | +use Rcsofttech85\FileHandler\FileEncryptor; |
| 10 | +use Rcsofttech85\FileHandler\Validator\FileValidatorTrait; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Helper\ProgressBar; |
| 13 | +use Symfony\Component\Console\Input\InputArgument; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Input\InputOption; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Console\SingleCommandApplication; |
| 18 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 19 | + |
| 20 | +const ENCRYPT = "encryption"; |
| 21 | +const DECRYPT = "decryption"; |
| 22 | + |
| 23 | +$command = (new SingleCommandApplication(name: 'file-encrypt')) |
| 24 | + ->addArgument('file', InputArgument::REQUIRED, 'file name to encrypt to decrypt') |
| 25 | + ->addOption( |
| 26 | + 'mode', |
| 27 | + null, |
| 28 | + InputOption::VALUE_REQUIRED, |
| 29 | + 'encrypt or decrypt mode', |
| 30 | + 'encryption' |
| 31 | + ) |
| 32 | + ->setCode(function (InputInterface $input, OutputInterface $output): int { |
| 33 | + $io = new SymfonyStyle($input, $output); |
| 34 | + |
| 35 | + $file = $input->getArgument('file'); |
| 36 | + $mode = $input->getOption('mode'); |
| 37 | + |
| 38 | + if (!in_array($mode, [ENCRYPT, DECRYPT])) { |
| 39 | + $io->error('invalid mode provided'); |
| 40 | + return Command::FAILURE; |
| 41 | + } |
| 42 | + $filValidator = (new class { |
| 43 | + use FileValidatorTrait; |
| 44 | + }); |
| 45 | + |
| 46 | + try { |
| 47 | + $filValidator->validateFileName($file); |
| 48 | + } catch (FileHandlerException) { |
| 49 | + $io->error( |
| 50 | + "{$file} does not exists" |
| 51 | + ); |
| 52 | + return Command::FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + $serviceContainer = new ServiceContainer(); |
| 56 | + |
| 57 | + /** @var FileEncryptor $encryptor */ |
| 58 | + $encryptor = $serviceContainer->getContainerBuilder()->get('file_encryptor'); |
| 59 | + try { |
| 60 | + $output->writeln("=================================="); |
| 61 | + $io->newLine(); |
| 62 | + $progressBar = new ProgressBar($output, 1); |
| 63 | + $progressBar->start(); |
| 64 | + $progressBar->setBarCharacter('<fg=yellow>█</>'); |
| 65 | + $progressBar->setEmptyBarCharacter("<fg=black>█</>"); |
| 66 | + $progressBar->setProgressCharacter("<fg=bright-blue>➤</>"); |
| 67 | + |
| 68 | + $progressBar->start(); |
| 69 | + |
| 70 | + $isEncrypted = false; |
| 71 | + |
| 72 | + if ($mode === DECRYPT) { |
| 73 | + $isEncrypted = $encryptor->decryptFile($file); |
| 74 | + } |
| 75 | + if ($mode === ENCRYPT) { |
| 76 | + $isEncrypted = $encryptor->encryptFile($file); |
| 77 | + } |
| 78 | + $progressBar->finish(); |
| 79 | + $io->newLine(); |
| 80 | + $output->writeln("=================================="); |
| 81 | + $io->newLine(); |
| 82 | + } catch (FileEncryptorException $e) { |
| 83 | + $io->newLine(); |
| 84 | + $io->error($e->getMessage()); |
| 85 | + return Command::FAILURE; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + if (!$isEncrypted) { |
| 90 | + $io->error("could not {$mode} file"); |
| 91 | + return Command::FAILURE; |
| 92 | + } |
| 93 | + |
| 94 | + $io->success("The file {$mode} was successful"); |
| 95 | + return Command::SUCCESS; |
| 96 | + })->run(); |
0 commit comments