Failed Conditions
Push — master ( 54f1b0...5f42b0 )
by Alexander
01:47
created

JiraApi::getIssueLinkTypeNames()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 17
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 1
b 0
f 0
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;
12
13
14
use chobie\Jira\Api;
15
use chobie\Jira\Api\Result;
16
use chobie\Jira\IssueType;
17
use Doctrine\Common\Cache\CacheProvider;
18
19
class JiraApi extends Api
20
{
21
22
	const CACHE_DURATION_ONE_MONTH = 2592000;
23
24
	/**
25
	 * Cache.
26
	 *
27
	 * @var CacheProvider
28
	 */
29
	protected $cache;
30
31
	/**
32
	 * Sets cache.
33
	 *
34
	 * @param CacheProvider $cache Cache.
35
	 *
36
	 * @return void
37
	 */
38 3
	public function setCache(CacheProvider $cache)
39
	{
40 3
		$this->cache = $cache;
41
	}
42
43
	/**
44
	 * Get fields definitions.
45
	 *
46
	 * @param integer $cache_duration Cache duration.
47
	 *
48
	 * @return array
49
	 */
50
	public function getFields($cache_duration = 0)
51
	{
52
		$cache_key = __METHOD__ . '()';
53
		$cached_value = $this->cache->fetch($cache_key);
54
55
		if ( $cached_value === false ) {
56
			$cached_value = parent::getFields();
57
			$this->cache->save($cache_key, $cached_value, $cache_duration);
58
		}
59
60
		return $cached_value;
61
	}
62
63
	/**
64
	 * Get available issue types.
65
	 *
66
	 * @param integer $cache_duration Cache duration.
67
	 *
68
	 * @return IssueType[]
69
	 */
70
	public function getIssueTypes($cache_duration = 0)
71
	{
72
		$cache_key = __METHOD__ . '()';
73
		$cached_value = $this->cache->fetch($cache_key);
74
75
		if ( $cached_value === false ) {
76
			$cached_value = parent::getIssueTypes();
77
			$this->cache->save($cache_key, $cached_value, $cache_duration);
78
		}
79
80
		return $cached_value;
81
	}
82
83
	/**
84
	 * Get versions of a project.
85
	 *
86
	 * @param string  $project_key    Project key.
87
	 * @param integer $cache_duration Cache duration.
88
	 *
89
	 * @return array|false
90
	 */
91
	public function getVersions($project_key, $cache_duration = 0)
92
	{
93
		$cache_key = __METHOD__ . '(' . $project_key . ')';
94
		$cached_value = $this->cache->fetch($cache_key);
95
96
		if ( $cached_value === false ) {
97
			$cached_value = parent::getVersions($project_key);
98
			$this->cache->save($cache_key, $cached_value, $cache_duration);
99
		}
100
101
		return $cached_value;
102
	}
103
104
	/**
105
	 * Returns possible link names.
106
	 *
107
	 * @return array
108
	 */
109
	public function getProjectKeys()
110
	{
111
		$cache_key = 'project_keys';
112
		$cached_value = $this->cache->fetch($cache_key);
113
114
		if ( $cached_value === false ) {
115
			$cached_value = array();
116
			$response = $this->getProjects();
117
118
			if ( $response instanceof Result ) {
119
				$response = $response->getResult();
120
			}
121
122
			foreach ( $response as $project_data ) {
123
				$cached_value[] = $project_data['key'];
124
			}
125
126
			$this->cache->save($cache_key, $cached_value);
127
		}
128
129
		return $cached_value;
130
	}
131
132
	/**
133
	 * Returns issue link type names.
134
	 *
135
	 * @return array
136
	 */
137
	public function getIssueLinkTypeNames()
138
	{
139
		$cache_key = 'issue_link_type_names';
140
		$cached_value = $this->cache->fetch($cache_key);
141
142
		if ( $cached_value === false ) {
143
			$cached_value = array();
144
			$response = $this->api(self::REQUEST_GET, '/rest/api/2/issueLinkType', array(), true);
145
146
			foreach ( $response['issueLinkTypes'] as $link_type_data ) {
147
				$cached_value[] = $link_type_data['name'];
148
			}
149
150
			$this->cache->save($cache_key, $cached_value);
151
		}
152
153
		return $cached_value;
154
	}
155
156
	/**
157
	 * Returns project component mapping (id to name).
158
	 *
159
	 * @param string  $project_key    Project key.
160
	 * @param integer $cache_duration Cache duration.
161
	 *
162
	 * @return array
163
	 */
164
	public function getProjectComponentMapping($project_key, $cache_duration = 0)
165
	{
166
		$cache_key = 'project_components[' . $project_key . ']';
167
		$cached_value = $this->cache->fetch($cache_key);
168
169
		if ( $cached_value === false ) {
170
			$cached_value = array();
171
			$project_components = $this->getProjectComponents($project_key);
172
173
			foreach ( $project_components as $project_component_data ) {
174
				$cached_value[$project_component_data['id']] = $project_component_data['name'];
175
			}
176
177
			$this->cache->save($cache_key, $cached_value, $cache_duration);
178
		}
179
180
		return $cached_value;
181
	}
182
183
}
184