Failed Conditions
Push — master ( 2cd9b5...479acf )
by Alexander
01:46
created

JiraApi::getIssueTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
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
	public function setCache(CacheProvider $cache)
39
	{
40
		$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 ) {
0 ignored issues
show
Bug introduced by
The expression $project_components of type array|object<chobie\Jira\Api\Result>|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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