Failed Conditions
Push — master ( 117895...2cd9b5 )
by Alexander
03:10
created

AbstractCommand::getProjectKeys()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 8.9197
c 1
b 0
f 0
cc 4
eloc 12
nc 3
nop 0
crap 20
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 chobie\Jira\Api;
15
use chobie\Jira\Api\Result;
16
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor;
17
use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand;
18
use Doctrine\Common\Cache\CacheProvider;
19
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
20
21
/**
22
 * Base command class.
23
 */
24
abstract class AbstractCommand extends BaseCommand
25
{
26
27
	/**
28
	 * Config editor.
29
	 *
30
	 * @var ConfigEditor
31
	 */
32
	private $_configEditor;
33
34
	/**
35
	 * Jira REST client.
36
	 *
37
	 * @var Api
38
	 */
39
	protected $jiraApi;
40
41
	/**
42
	 * Cache.
43
	 *
44
	 * @var CacheProvider
45
	 */
46
	protected $cache;
47
48
	/**
49
	 * Prepare dependencies.
50
	 *
51
	 * @return void
52
	 */
53
	protected function prepareDependencies()
54
	{
55
		$container = $this->getContainer();
56
57
		$this->_configEditor = $container['config_editor'];
58
		$this->jiraApi = $container['jira_api'];
59
		$this->cache = $container['cache'];
60
	}
61
62
	/**
63
	 * Returns command setting value.
64
	 *
65
	 * @param string $name Name.
66
	 *
67
	 * @return mixed
68
	 */
69
	protected function getSetting($name)
70
	{
71
		return $this->_configEditor->get($name);
72
	}
73
74
	/**
75
	 * Checks if an issue key is valid.
76
	 *
77
	 * @param string $issue_key Issue key.
78
	 *
79
	 * @return boolean
80
	 */
81
	public function isValidIssueKey($issue_key)
82
	{
83
		return preg_match('/^([A-Z]+-[0-9]+)$/', $issue_key);
84
	}
85
86
	/**
87
	 * Return possible values for the named argument
88
	 *
89
	 * @param string            $argumentName Argument name.
90
	 * @param CompletionContext $context      Completion context.
91
	 *
92
	 * @return array
93
	 */
94
	public function completeArgumentValues($argumentName, CompletionContext $context)
95
	{
96
		$ret = parent::completeArgumentValues($argumentName, $context);
97
98
		if ( $argumentName === 'project_key' ) {
99
			return $this->getProjectKeys();
100
		}
101
102
		return $ret;
103
	}
104
105
	/**
106
	 * Returns possible link names.
107
	 *
108
	 * @return array
109
	 */
110
	protected function getProjectKeys()
111
	{
112
		$cache_key = 'projects';
113
		$cached_value = $this->cache->fetch($cache_key);
114
115
		if ( $cached_value === false ) {
116
			$cached_value = array();
117
			$response = $this->jiraApi->getProjects();
118
119
			if ( $response instanceof Result ) {
120
				$response = $response->getResult();
121
			}
122
123
			foreach ( $response as $project_data ) {
124
				$cached_value[] = $project_data['key'];
125
			}
126
127
			$this->cache->save($cache_key, $cached_value);
128
		}
129
130
		return $cached_value;
131
	}
132
133
}
134