Passed
Push — master ( cfd7bb...829e1a )
by Alexander
10:18
created

AbstractCommand::showStatistics()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
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\Config\ConfigEditor;
15
use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand;
16
use ConsoleHelpers\JiraCLI\JiraApi;
17
use Doctrine\Common\Cache\CacheProvider;
18
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
19
20
/**
21
 * Base command class.
22
 */
23
abstract class AbstractCommand extends BaseCommand
24
{
25
26
	/**
27
	 * Config editor.
28
	 *
29
	 * @var ConfigEditor
30
	 */
31
	private $_configEditor;
32
33
	/**
34
	 * Jira REST client.
35
	 *
36
	 * @var JiraApi
37
	 */
38
	protected $jiraApi;
39
40
	/**
41
	 * Cache.
42
	 *
43
	 * @var CacheProvider
44
	 */
45
	protected $cache;
46
47
	/**
48
	 * Prepare dependencies.
49
	 *
50
	 * @return void
51
	 */
52
	protected function prepareDependencies()
53
	{
54
		$container = $this->getContainer();
55
56
		$this->_configEditor = $container['config_editor'];
57
		$this->jiraApi = $container['jira_api'];
58
		$this->cache = $container['cache'];
59
	}
60
61
	/**
62
	 * Returns command setting value.
63
	 *
64
	 * @param string $name Name.
65
	 *
66
	 * @return mixed
67
	 */
68
	protected function getSetting($name)
69
	{
70
		return $this->_configEditor->get($name);
71
	}
72
73
	/**
74
	 * Checks if an issue key is valid.
75
	 *
76
	 * @param string $issue_key Issue key.
77
	 *
78
	 * @return boolean
79
	 */
80
	public function isValidIssueKey($issue_key)
81
	{
82
		return preg_match('/^([A-Z]+-[0-9]+)$/', $issue_key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^([A...[0-9]+)$/', $issue_key) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
83
	}
84
85
	/**
86
	 * Return possible values for the named argument
87
	 *
88
	 * @param string            $argumentName Argument name.
89
	 * @param CompletionContext $context      Completion context.
90
	 *
91
	 * @return array
92
	 */
93
	public function completeArgumentValues($argumentName, CompletionContext $context)
94
	{
95
		$ret = parent::completeArgumentValues($argumentName, $context);
96
97
		if ( $argumentName === 'project_key' || $argumentName === 'project_keys' ) {
98
			return $this->jiraApi->getProjectKeys();
99
		}
100
101
		return $ret;
102
	}
103
104
	/**
105
	 * Shows statistics.
106
	 *
107
	 * @return void
108
	 */
109
	protected function showStatistics()
110
	{
111
		if ( !$this->io->isVerbose() ) {
112
			return;
113
		}
114
115
		$request_count = $this->jiraApi->getRequestCount();
116
117
		if ( $request_count ) {
118
			$this->io->writeln('===============');
119
			$this->io->writeln('API Requests Made: ' . $request_count);
120
		}
121
	}
122
123
}
124