Get   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 9.33 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 6
dl 7
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
B execute() 0 16 5
A _formatPasswords() 0 9 2
A _error() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Nubs\PwMan\Command;
3
4
use GnuPG;
5
use Nubs\PwMan\PasswordFile;
6
use Nubs\PwMan\PasswordManager;
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\ConsoleOutput;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * A symonfy console command to get passwords from a password file.
16
 */
17
class Get extends Command
18
{
19
    /**
20
     * Configures the command's options.
21
     *
22
     * @return void
23
     */
24
    protected function configure()
25
    {
26
        $this->setName('get')
27
            ->setDescription('Get password(s) for the specified application(s)')
28
            ->addArgument('password-file', InputArgument::REQUIRED, 'The path to the encrypted password file')
29
            ->addArgument('application', InputArgument::OPTIONAL, 'The application(s) to query')
30
            ->addOption('decrypt-key', 'd', InputOption::VALUE_REQUIRED, 'The uid or fingerprint for the decryption key')
31
            ->addOption('decrypt-passphrase', 'y', InputOption::VALUE_REQUIRED, 'The passphrase for the decryption key')
32
            ->addOption('output-format', 'f', InputOption::VALUE_REQUIRED, 'The output format (valid: json)', 'json');
33
    }
34
35
    /**
36
     * Gets the password(s) for the specified application(s).
37
     *
38
     * @param \Symfony\Component\Console\Input\InputInterface $input The command input.
39
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output.
40
     * @return int The return status
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $passwordFile = new PasswordFile($input->getArgument('password-file'), new GnuPG());
45
        $passwordFile->addDecryptKey($input->getOption('decrypt-key') ?: '', $input->getOption('decrypt-passphrase') ?: '');
46
        $passwords = $passwordFile->getPasswords();
47
        if ($passwords === null) {
48
            return $this->_error($output, 'Failed to load passwords from file!');
49
        }
50
51
        $passwordManager = new PasswordManager($passwords);
52
        $application = $input->getArgument('application') ?: '';
53
54
        $matchingPasswords = $passwordManager->matchingApplication($application);
55
56
        $output->writeln($this->_formatPasswords($matchingPasswords, $input->getOption('output-format')));
57
    }
58
59
    /**
60
     * Format the passwords according to the output format.
61
     *
62
     * @param array<array> $passwords The passwords to format.
63
     * @param string $outputFormat The output format (valid: json).
64
     * @return string The formatted passwords for display.
65
     */
66
    private function _formatPasswords(array $passwords, $outputFormat)
67
    {
68
        switch ($outputFormat) {
69
            case 'json':
70
                return json_encode($passwords, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
71
            default:
72
                throw new InvalidArgumentException("Invalid format: {$outputFormat}");
73
        }
74
    }
75
76
    /**
77
     * Prints an error message and returns the given error code.
78
     *
79
     * @param \Symfony\Component\Console\Output\OutputInterface $output The command output.
80
     * @param string $message The message to output.
81
     * @param int $code The return status.
82
     * @return int The return status
83
     */
84 View Code Duplication
    private function _error(OutputInterface $output, $message, $code = 1)
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...
85
    {
86
        $stderr = $output instanceof ConsoleOutput ? $output->getErrorOutput() : $output;
87
        $stderr->writeln("<error>{$message}</error>");
88
89
        return $code;
90
    }
91
}
92