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

JiraApi::getRequestCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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;
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
	 * Request counter.
33
	 *
34
	 * @var integer
35
	 */
36
	protected $requestCounter = 0;
37
38
	/**
39
	 * @inheritDoc
40
	 */
41
	public function api(
42
		$method = self::REQUEST_GET,
43
		$url,
44
		$data = array(),
45
		$return_as_array = false,
46
		$is_file = false,
47
		$debug = false
48
	) {
49
		$this->requestCounter++;
50
51
		return parent::api($method, $url, $data, $return_as_array, $is_file, $debug);
52
	}
53
54
	/**
55
	 * Returns request counter.
56
	 *
57
	 * @return integer
58
	 */
59
	public function getRequestCount()
60
	{
61
		return $this->requestCounter;
62
	}
63
64
	/**
65
	 * Sets cache.
66
	 *
67
	 * @param CacheProvider $cache Cache.
68
	 *
69
	 * @return void
70
	 */
71 3
	public function setCache(CacheProvider $cache)
72
	{
73 3
		$this->cache = $cache;
74
	}
75
76
	/**
77
	 * Get fields definitions.
78
	 *
79
	 * @param integer $cache_duration Cache duration.
80
	 *
81
	 * @return array
82
	 */
83
	public function getFields($cache_duration = 0)
84
	{
85
		$cache_key = __METHOD__ . '()';
86
		$cached_value = $this->cache->fetch($cache_key);
87
88
		if ( $cached_value === false ) {
89
			$cached_value = parent::getFields();
90
			$this->cache->save($cache_key, $cached_value, $cache_duration);
91
		}
92
93
		return $cached_value;
94
	}
95
96
	/**
97
	 * Get available issue types.
98
	 *
99
	 * @param integer $cache_duration Cache duration.
100
	 *
101
	 * @return IssueType[]
102
	 */
103
	public function getIssueTypes($cache_duration = 0)
104
	{
105
		$cache_key = __METHOD__ . '()';
106
		$cached_value = $this->cache->fetch($cache_key);
107
108
		if ( $cached_value === false ) {
109
			$cached_value = parent::getIssueTypes();
110
			$this->cache->save($cache_key, $cached_value, $cache_duration);
111
		}
112
113
		return $cached_value;
114
	}
115
116
	/**
117
	 * Get versions of a project.
118
	 *
119
	 * @param string  $project_key    Project key.
120
	 * @param integer $cache_duration Cache duration.
121
	 *
122
	 * @return array|false
123
	 */
124
	public function getVersions($project_key, $cache_duration = 0)
125
	{
126
		$cache_key = __METHOD__ . '(' . $project_key . ')';
127
		$cached_value = $this->cache->fetch($cache_key);
128
129
		if ( $cached_value === false ) {
130
			$cached_value = parent::getVersions($project_key);
131
			$this->cache->save($cache_key, $cached_value, $cache_duration);
132
		}
133
134
		return $cached_value;
135
	}
136
137
	/**
138
	 * Returns possible link names.
139
	 *
140
	 * @return array
141
	 */
142
	public function getProjectKeys()
143
	{
144
		$cache_key = 'project_keys';
145
		$cached_value = $this->cache->fetch($cache_key);
146
147
		if ( $cached_value === false ) {
148
			$cached_value = array();
149
			$response = $this->getProjects();
150
151
			if ( $response instanceof Result ) {
152
				$response = $response->getResult();
153
			}
154
155
			foreach ( $response as $project_data ) {
156
				$cached_value[] = $project_data['key'];
157
			}
158
159
			$this->cache->save($cache_key, $cached_value);
160
		}
161
162
		return $cached_value;
163
	}
164
165
	/**
166
	 * Returns issue link type names.
167
	 *
168
	 * @return array
169
	 */
170
	public function getIssueLinkTypeNames()
171
	{
172
		$cache_key = 'issue_link_type_names';
173
		$cached_value = $this->cache->fetch($cache_key);
174
175
		if ( $cached_value === false ) {
176
			$cached_value = array();
177
			$response = $this->api(self::REQUEST_GET, '/rest/api/2/issueLinkType', array(), true);
178
179
			foreach ( $response['issueLinkTypes'] as $link_type_data ) {
180
				$cached_value[] = $link_type_data['name'];
181
			}
182
183
			$this->cache->save($cache_key, $cached_value);
184
		}
185
186
		return $cached_value;
187
	}
188
189
	/**
190
	 * Returns project component mapping (id to name).
191
	 *
192
	 * @param string  $project_key    Project key.
193
	 * @param integer $cache_duration Cache duration.
194
	 *
195
	 * @return array
196
	 */
197
	public function getProjectComponentMapping($project_key, $cache_duration = 0)
198
	{
199
		$cache_key = 'project_components[' . $project_key . ']';
200
		$cached_value = $this->cache->fetch($cache_key);
201
202
		if ( $cached_value === false ) {
203
			$cached_value = array();
204
			$project_components = $this->getProjectComponents($project_key);
205
206
			foreach ( $project_components as $project_component_data ) {
207
				$cached_value[$project_component_data['id']] = $project_component_data['name'];
208
			}
209
210
			$this->cache->save($cache_key, $cached_value, $cache_duration);
211
		}
212
213
		return $cached_value;
214
	}
215
216
}
217