Skip to content

Commit d34781e

Browse files
authored
File encrypt (#53)
Signed-off-by: rahul <rcsofttech85@gmail.com>
1 parent d09273a commit d34781e

File tree

4 files changed

+235
-34
lines changed

4 files changed

+235
-34
lines changed

bin/file-encrypt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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();

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"symfony/console": "^6.3",
2525
"symfony/dependency-injection": "^6.3",
2626
"symfony/config": "^6.3",
27-
"symfony/yaml": "^6.3"
27+
"symfony/yaml": "^6.3",
28+
"symfony/process": "^6.3"
2829
},
2930
"require-dev": {
3031
"phpunit/phpunit": "^10",
@@ -36,6 +37,7 @@
3637
"bin": [
3738
"bin/file-diff",
3839
"bin/view-csv",
39-
"bin/view-json"
40+
"bin/view-json",
41+
"bin/file-encrypt"
4042
]
4143
}

composer.lock

Lines changed: 93 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Integration;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\Process\Process;
8+
9+
class FileEncryptCommandTest extends TestCase
10+
{
11+
#[Test]
12+
public function fileIsEncryptedProperly(): void
13+
{
14+
file_put_contents("dummy.txt", "this content should get encrypted");
15+
16+
$command = "bin/file-encrypt dummy.txt";
17+
$process = Process::fromShellCommandline($command);
18+
$process->run();
19+
20+
21+
$actualOutput = $process->getOutput();
22+
$exitCode = $process->getExitCode();
23+
24+
$this->assertStringContainsString("The file encryption was successful", $actualOutput);
25+
$this->assertSame(0, $exitCode);
26+
}
27+
28+
#[Test]
29+
public function fileIsDecryptedProperly(): void
30+
{
31+
$command = "bin/file-encrypt dummy.txt --mode=decryption";
32+
$process = Process::fromShellCommandline($command);
33+
$process->run();
34+
35+
36+
$actualOutput = $process->getOutput();
37+
$exitCode = $process->getExitCode();
38+
39+
$this->assertStringContainsString("The file decryption was successful", $actualOutput);
40+
$this->assertSame(0, $exitCode);
41+
}
42+
}

0 commit comments

Comments
 (0)