1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdelespierre\PhpPhash\Command; |
4
|
|
|
|
5
|
|
|
use Bdelespierre\PhpPhash\Command\ValidatesSamplingSize; |
6
|
|
|
use Bdelespierre\PhpPhash\PHash; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class Generate extends Command |
14
|
|
|
{ |
15
|
|
|
use ValidatesSamplingSize; |
16
|
|
|
|
17
|
|
|
protected $phash; |
18
|
|
|
|
19
|
|
|
public function __construct(PHash $phash) |
20
|
|
|
{ |
21
|
|
|
$this->phash = $phash; |
22
|
|
|
|
23
|
|
|
parent::__construct(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->setName('generate') |
29
|
|
|
->setDescription('Generates the pHash of given image') |
30
|
|
|
->addArgument('file', InputArgument::REQUIRED, 'Pass the file.') |
31
|
|
|
->addOption('size', 's', InputOption::VALUE_REQUIRED, 'Sampling size.', 8) |
32
|
|
|
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format [hex,bin,ascii].', 'hex'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
$this->validate($input); |
39
|
|
|
} catch (\InvalidArgumentException $e) { |
40
|
|
|
$output->writeln("<error>{$e->getMessage()}</error>"); |
41
|
|
|
return Command::FAILURE; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$bits = $this->phash->hash( |
45
|
|
|
new \SplFileInfo($input->getArgument('file')), |
46
|
|
|
$input->getOption('size') |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$this->display($output, $input->getOption('format'), $bits, $input->getOption('size')); |
50
|
|
|
|
51
|
|
|
return Command::SUCCESS; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function validate(InputInterface $input) |
55
|
|
|
{ |
56
|
|
|
if (! is_readable($input->getArgument('file'))) { |
57
|
|
|
throw new \InvalidArgumentException("File {$input->getArgument('file')} not found or unreadable"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->validateSamplingSize($input->getOption('size')); |
61
|
|
|
|
62
|
|
|
if (! in_array($input->getOption('format'), ['hex', 'bin', 'ascii'])) { |
63
|
|
|
throw new \InvalidArgumentException("Invalid format"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function display(OutputInterface $output, string $format, string $bits, int $size) |
68
|
|
|
{ |
69
|
|
|
switch ($format) { |
70
|
|
|
default: |
71
|
|
|
case 'bin': |
72
|
|
|
$output->writeln($bits); |
73
|
|
|
break; |
74
|
|
|
|
75
|
|
|
case 'hex': |
76
|
|
|
$output->writeln( |
77
|
|
|
strlen($bits) <= (PHP_INT_SIZE * 8) |
78
|
|
|
? base_convert($bits, 2, 16) |
79
|
|
|
: gmp_strval(gmp_init($bits, 2), 16) |
80
|
|
|
); |
81
|
|
|
break; |
82
|
|
|
|
83
|
|
|
case 'ascii': |
84
|
|
|
$output->writeln( |
85
|
|
|
implode("\n", array_map( |
86
|
|
|
function ($str) { |
87
|
|
|
return preg_replace('/(1+)/', '<fg=green>$1</>', $str); |
88
|
|
|
}, |
89
|
|
|
str_split($bits, $size) |
90
|
|
|
)) |
91
|
|
|
); |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.