Completed
Push — master ( dc3cca...ad625a )
by KwangSeob
04:42 queued 02:12
created

VersionService::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\Version;
4
5
use JiraRestApi\Issue\Version;
6
use JiraRestApi\Issue\VersionUnresolvedCount;
7
use JiraRestApi\Issue\VersionIssueCounts;
8
use JiraRestApi\JiraException;
9
use JiraRestApi\Project\ProjectService;
10
11
class VersionService extends \JiraRestApi\JiraClient
12
{
13
    private $uri = '/version';
14
15
    /**
16
     * Function to create a new project version.
17
     *
18
     * @param Version|array $version
19
     *
20
     * @throws \JiraRestApi\JiraException
21
     * @throws \JsonMapper_Exception
22
     *
23
     * @return Version Version class
24
     */
25
    public function create($version)
26
    {
27
        if ($version->releaseDate instanceof \DateTime) {
28
            $version->releaseDate = $version->releaseDate->format('Y-m-d');
0 ignored issues
show
Documentation Bug introduced by
It seems like $version->releaseDate->format('Y-m-d') of type string is incompatible with the declared type null|DateTime of property $releaseDate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        }
30
        $data = json_encode($version);
31
32
        $this->log->addInfo("Create Version=\n".$data);
33
34
        $ret = $this->exec($this->uri, $data, 'POST');
35
36
        return $this->json_mapper->map(
37
            json_decode($ret), new Version()
38
        );
39
    }
40
41
    /**
42
     * Modify a version's sequence within a project.
43
     *
44
     * @param $version
45
     *
46
     * @throws JiraException
47
     */
48
    public function move($version)
0 ignored issues
show
Unused Code introduced by
The parameter $version is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
    public function move(/** @scrutinizer ignore-unused */ $version)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        throw new JiraException('move version not yet implemented');
51
    }
52
53
    /**
54
     * get project version.
55
     *
56
     * @param $id version id
57
     *
58
     * @return Version
59
     *
60
     * @see ProjectService::getVersions()
61
     */
62
    public function get($id)
63
    {
64
        $ret = $this->exec($this->uri.'/'.$id);
65
66
        $this->log->addInfo('Result='.$ret);
67
68
        $json = json_decode($ret);
69
        $results = array_map(function ($elem) {
70
            return $this->json_mapper->map($elem, new ProjectType());
0 ignored issues
show
Bug introduced by
The type JiraRestApi\Version\ProjectType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
        }, $json);
72
73
        return $results;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $results returns the type array which is incompatible with the documented return type JiraRestApi\Issue\Version.
Loading history...
74
    }
75
76
    /**
77
     * @author Martijn Smidt <[email protected]>
78
     *
79
     * @param Version $version
80
     *
81
     * @throws JiraException
82
     *
83
     * @return Version
84
     */
85
    public function update(Version $version)
86
    {
87
        if (!$version->id || !is_numeric($version->id)) {
88
            throw new JiraException($version->id.' is not a valid version id.');
89
        }
90
91
        if ($version->releaseDate instanceof \DateTime) {
92
            $version->releaseDate = $version->releaseDate->format('Y-m-d');
0 ignored issues
show
Documentation Bug introduced by
It seems like $version->releaseDate->format('Y-m-d') of type string is incompatible with the declared type null|DateTime of property $releaseDate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
93
        }
94
95
        //Only one of 'releaseDate' and 'userReleaseDate' can be specified when editing a version."
96
        $version->userReleaseDate = null;
97
98
        $data = json_encode($version);
99
        $ret = $this->exec($this->uri.'/'.$version->id, $data, 'PUT');
100
101
        return $this->json_mapper->map(
102
            json_decode($ret), new Version()
103
        );
104
    }
105
106
    /**
107
     * @author Martijn Smidt <[email protected]>
108
     *
109
     * @param Version      $version
110
     * @param Version|bool $moveAffectedIssuesTo
111
     * @param Version|bool $moveFixIssuesTo
112
     *
113
     * @throws JiraException
114
     *
115
     * @return bool
116
     */
117
    public function delete(Version $version, $moveAffectedIssuesTo = false, $moveFixIssuesTo = false)
118
    {
119
        if (!$version->id || !is_numeric($version->id)) {
120
            throw new JiraException($version->id.' is not a valid version id.');
121
        }
122
123
        $data = [];
124
125
        if ($moveAffectedIssuesTo && $moveAffectedIssuesTo instanceof Version) {
126
            $data['moveAffectedIssuesTo'] = $moveAffectedIssuesTo->name;
127
        }
128
129
        if ($moveFixIssuesTo && $moveFixIssuesTo instanceof Version) {
130
            $data['moveFixIssuesTo'] = $moveFixIssuesTo->name;
131
        }
132
133
        $ret = $this->exec($this->uri.'/'.$version->id, json_encode($data), 'DELETE');
134
135
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type string which is incompatible with the documented return type boolean.
Loading history...
136
    }
137
138
    public function merge($ver)
0 ignored issues
show
Unused Code introduced by
The parameter $ver is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    public function merge(/** @scrutinizer ignore-unused */ $ver)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        throw new JiraException('merge version not yet implemented');
141
    }
142
143
    /**
144
     * Returns a bean containing the number of fixed in and affected issues for the given version.
145
     *
146
     * @param Version $version
147
     *
148
     * @throws JiraException
149
     *
150
     * @see https://docs.atlassian.com/jira/REST/server/#api/2/version-getVersionRelatedIssues
151
     */
152
    public function getRelatedIssues(Version $version)
153
    {
154
        if (!$version->id || !is_numeric($version->id)) {
155
            throw new JiraException($version->id.' is not a valid version id.');
156
        }
157
158
        $ret = $this->exec($this->uri.'/'.$version->id.'/relatedIssueCounts');
159
160
        return $this->json_mapper->map(
161
            json_decode($ret),
162
            new VersionIssueCounts()
163
        );
164
    }
165
166
    /**
167
     * Returns a bean containing the number of unresolved issues for the given version.
168
     *
169
     * @param Version $version
170
     *
171
     * @throws JiraException
172
     *
173
     * @see https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/version-getVersionUnresolvedIssues
174
     *
175
     * @return VersionUnresolvedCount
176
     */
177
    public function getUnresolvedIssues(Version $version)
178
    {
179
        if (!$version->id || !is_numeric($version->id)) {
180
            throw new JiraException($version->id.' is not a valid version id.');
181
        }
182
183
        $ret = $this->exec($this->uri.'/'.$version->id.'/unresolvedIssueCount');
184
185
        return $this->json_mapper->map(
186
            json_decode($ret),
187
            new VersionUnresolvedCount()
188
        );
189
    }
190
}
191