DownloadAttachmentCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 2
rs 9.9666
1
<?php
2
/**
3
 * This file is part of the Jira-CLI library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/jira-cli
9
 */
10
11
namespace ConsoleHelpers\JiraCLI\Command;
12
13
14
use ConsoleHelpers\ConsoleKit\Exception\CommandException;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class DownloadAttachmentCommand extends AbstractCommand
21
{
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	protected function configure()
27
	{
28
		$this
29
			->setName('download-attachment')
30
			->setDescription('Downloads issue attachments')
31
			->addArgument('issue', InputArgument::REQUIRED, 'Issue key or url')
32
			->addOption(
33
				'index',
34
				'i',
35
				InputOption::VALUE_IS_ARRAY + InputOption::VALUE_OPTIONAL,
36
				'Attachment index to download'
37
			);
38
	}
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	protected function execute(InputInterface $input, OutputInterface $output)
44
	{
45
		$issue_key = $this->getIssueKey();
46
		$issue_data = $this->jiraApi->getIssue($issue_key)->getResult();
47
48
		$attachments = $issue_data['fields']['attachment'];
49
50
		// Show attachments.
51
		$this->io->writeln('<info>Issue ' . $issue_key . ' attachments:</info>');
52
53
		foreach ( $attachments as $index => $attachment_data ) {
54
			$this->io->writeln($index . '. ' . $attachment_data['filename']);
55
		}
56
57
		$indexes = $this->io->getOption('index');
58
59
		if ( count($indexes) === 0 ) {
0 ignored issues
show
Bug introduced by
It seems like $indexes can also be of type boolean and null and string; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
		if ( count(/** @scrutinizer ignore-type */ $indexes) === 0 ) {
Loading history...
60
			$indexes = array_keys($attachments);
61
		}
62
63
		// Download attachments.
64
		$this->io->writeln('');
65
		$this->io->writeln('Downloading all attachments to <info>' . getcwd() . '</info>');
66
67
		foreach ( $attachments as $index => $attachment_data ) {
68
			if ( !in_array($index, $indexes) ) {
0 ignored issues
show
Bug introduced by
It seems like $indexes can also be of type boolean and null and string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
			if ( !in_array($index, /** @scrutinizer ignore-type */ $indexes) ) {
Loading history...
69
				continue;
70
			}
71
72
			$this->io->write('- ' . $attachment_data['filename'] . ' ... ');
73
			file_put_contents(
74
				$attachment_data['filename'],
75
				$this->jiraApi->downloadAttachment($attachment_data['content'])
76
			);
77
			$this->io->writeln('done');
78
		}
79
80
		$this->showStatistics();
81
	}
82
83
	/**
84
	 * Returns issue key.
85
	 *
86
	 * @return string
87
	 * @throws CommandException When issue key is invalid.
88
	 */
89
	protected function getIssueKey()
90
	{
91
		$issue = $this->io->getArgument('issue');
92
93
		if ( $this->isValidIssueKey($issue) ) {
0 ignored issues
show
Bug introduced by
It seems like $issue can also be of type string[]; however, parameter $issue_key of ConsoleHelpers\JiraCLI\C...mand::isValidIssueKey() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
		if ( $this->isValidIssueKey(/** @scrutinizer ignore-type */ $issue) ) {
Loading history...
94
			return $issue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $issue also could return the type string[] which is incompatible with the documented return type string.
Loading history...
95
		}
96
97
		throw new CommandException('The issue key is invalid');
98
	}
99
100
}
101