Generate::display()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.1608
c 0
b 0
f 0
cc 5
nc 4
nop 4
1
<?php
2
3
namespace Bdelespierre\PhpPhash\Command;
4
5
use Bdelespierre\PhpPhash\PHash;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class Generate extends Command
13
{
14
    use ValidatesSamplingSize;
15
16
    protected $phash;
17
18
    public function __construct(PHash $phash)
19
    {
20
        $this->phash = $phash;
21
22
        parent::__construct();
23
    }
24
25 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
26
    {
27
        $this->setName('generate')
28
            ->setDescription('Generates the pHash of given image')
29
            ->addArgument('file', InputArgument::REQUIRED, 'Pass the file.')
30
            ->addOption('size', 's', InputOption::VALUE_REQUIRED, 'Sampling size.', 8)
31
            ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format [hex,bin,ascii].', 'hex');
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        try {
37
            $this->validate($input);
38
        } catch (\InvalidArgumentException $e) {
39
            $output->writeln("<error>{$e->getMessage()}</error>");
40
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