IssueRepository::reOpenIssue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/*
3
 * This file is part of the GitlabIssueBundle package.
4
 *
5
 * (c) Paul Schweppe <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace VersionControl\GitlabIssueBundle\Repository;
12
13
use VersionControl\GitControlBundle\Repository\Issues\IssueRepositoryInterface;
14
use VersionControl\GitlabIssueBundle\Entity\Issues\Issue;
15
use VersionControl\GitlabIssueBundle\Entity\Issues\IssueComment;
16
use VersionControl\GitlabIssueBundle\DataTransformer\IssueToEntityTransformer;
17
use VersionControl\GitlabIssueBundle\DataTransformer\IssueCommentToEntityTransformer;
18
19
class IssueRepository extends GitlabBase implements IssueRepositoryInterface
20
{
21
    /**
22
     * Finds issues for a state.
23
     *
24
     * @param string $keyword
25
     *
26
     * @return array of issues
27
     */
28
    public function findIssues($keyword = '', $state = 'opened')
29
    {
30
        //$project = new \Gitlab\Model\Project($this->issueIntegrator->getProjectName(), $this->client);
31
        if ($state === 'open') {
32
            $state = 'opened';
33
        }
34
        $this->authenticate();
35
        $issues = $this->client->api('issues')->all($this->issueIntegrator->getProjectName(), 1, 20, array('state' => $state));
0 ignored issues
show
Bug introduced by
The method getProjectName() does not exist on VersionControl\GitContro...\ProjectIssueIntegrator. Did you maybe mean getProject()? ( Ignorable by Annotation )

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

35
        $issues = $this->client->api('issues')->all($this->issueIntegrator->/** @scrutinizer ignore-call */ getProjectName(), 1, 20, array('state' => $state));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37
        return $this->mapIssues($issues);
0 ignored issues
show
Bug introduced by
$issues of type string is incompatible with the type array expected by parameter $issues of VersionControl\GitlabIss...Repository::mapIssues(). ( Ignorable by Annotation )

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

37
        return $this->mapIssues(/** @scrutinizer ignore-type */ $issues);
Loading history...
38
    }
39
40
    public function countFindIssues($keyword, $state = 'open')
41
    {
42
        $this->authenticate();
43
        if ($state === 'open') {
44
            $state = 'opened';
45
        }
46
        $issues = $this->client->api('issues')->all($this->issueIntegrator->getProjectName(), 1, 20, array('state' => $state));
47
48
        return count($issues);
0 ignored issues
show
Bug introduced by
$issues of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

48
        return count(/** @scrutinizer ignore-type */ $issues);
Loading history...
49
    }
50
51
    /**
52
     * @param int $id
53
     */
54
    public function findIssueById($id)
55
    {
56
        $this->authenticate();
57
        $issue = $this->client->api('issues')->show($this->issueIntegrator->getProjectName(), $id);
58
        $issueComments = $this->client->api('issues')->showComments($this->issueIntegrator->getProjectName(), $id);
59
60
        return $this->mapIssueToEntity($issue, $issueComments);
61
    }
62
63
    /**
64
     * Gets a new Issue entity.
65
     *
66
     * @param type $issue
0 ignored issues
show
Bug introduced by
The type VersionControl\GitlabIssueBundle\Repository\type 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...
67
     *
68
     * @return VersionControl\GitControlBundle\Entity\Issues\Issue
0 ignored issues
show
Bug introduced by
The type VersionControl\GitlabIss...dle\Entity\Issues\Issue was not found. Did you mean VersionControl\GitContro...dle\Entity\Issues\Issue? If so, make sure to prefix the type with \.
Loading history...
69
     */
70
    public function newIssue()
71
    {
72
        $issueEntity = new Issue();
73
74
        return $issueEntity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $issueEntity returns the type VersionControl\GitlabIss...dle\Entity\Issues\Issue which is incompatible with the documented return type VersionControl\GitlabIss...dle\Entity\Issues\Issue.
Loading history...
75
    }
76
77
    /**
78
     * @param type $issueEntity
79
     */
80
    public function createIssue($issueEntity)
81
    {
82
        $this->authenticate();
83
84
        $issue = $this->client->api('issues')->create($this->issueIntegrator->getProjectName(), $this->mapEntityToIssue($issueEntity));
85
86
        return $this->mapIssueToEntity($issue);
87
    }
88
89
    /**
90
     * @param int $id
91
     */
92
    public function reOpenIssue($id)
93
    {
94
        $this->authenticate();
95
        $issue = $this->client->api('issues')->update($this->issueIntegrator->getProjectName(), $id, array('state_event' => 'reopen'));
96
97
        return $this->mapIssueToEntity($issue);
98
    }
99
100
    /**
101
     * @param int $id
102
     */
103
    public function closeIssue($id)
104
    {
105
        $this->authenticate();
106
        $issue = $this->client->api('issues')->update($this->issueIntegrator->getProjectName(), $id, array('state_event' => 'close'));
107
108
        return $this->mapIssueToEntity($issue);
109
    }
110
111
    /**
112
     * @param int $issueEntity
113
     */
114
    public function updateIssue($issueEntity)
115
    {
116
        $this->authenticate();
117
        $this->client->api('issues')->update($this->issueIntegrator->getProjectName(), $issueEntity->getId(), $this->mapEntityToIssue($issueEntity));
0 ignored issues
show
Bug introduced by
The method getId() does not exist on integer. ( Ignorable by Annotation )

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

117
        $this->client->api('issues')->update($this->issueIntegrator->getProjectName(), $issueEntity->/** @scrutinizer ignore-call */ getId(), $this->mapEntityToIssue($issueEntity));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
    }
119
120
    public function addlabel($issueEntity, $labelEntity)
121
    {
122
        $this->authenticate();
123
        $labels = $this->client->api('issues')->labels()->add($this->issueIntegrator->getProjectName(), $issueEntity->getId(), $labelEntity->getTitle());
0 ignored issues
show
Unused Code introduced by
The assignment to $labels is dead and can be removed.
Loading history...
Bug introduced by
The method labels() does not exist on Gitlab\Api\Issues. ( Ignorable by Annotation )

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

123
        $labels = $this->client->api('issues')->/** @scrutinizer ignore-call */ labels()->add($this->issueIntegrator->getProjectName(), $issueEntity->getId(), $labelEntity->getTitle());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
    }
125
126
    /**
127
     * Gets the number of Issues for a milestone by state.
128
     *
129
     * @param int    $issueMilestoneId
130
     * @param string $state            open|closed|blank
131
     */
132
    public function countIssuesInMilestones($issueMilestoneId, $state)
133
    {
134
        $openCount = 0;
135
        $closedCount = 0;
136
        $this->authenticate();
137
        $issues = $this->client->api('milestones')->issues($this->issueIntegrator->getProjectName(), $issueMilestoneId);
138
        foreach ($issues as $issue) {
0 ignored issues
show
Bug introduced by
The expression $issues of type string is not traversable.
Loading history...
139
            if ($issue['state'] === 'opened') {
140
                ++$openCount;
141
            } elseif ($issue['state'] === 'closed') {
142
                ++$closedCount;
143
            }
144
        }
145
        if ($state === 'open') {
146
            $count = $openCount;
147
        } elseif ($state === 'closed') {
148
            $count = $closedCount;
149
        } else {
150
            $count = count($issues);
0 ignored issues
show
Bug introduced by
$issues of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

150
            $count = count(/** @scrutinizer ignore-type */ $issues);
Loading history...
151
        }
152
153
        return $count;
154
    }
155
156
    /**
157
     * Find issues in milestone.
158
     *
159
     * @param int    $issueMilestoneId
160
     * @param string $state            open|closed|blank
161
     * @param string $keyword          Search string
162
     */
163
    public function findIssuesInMilestones($issueMilestoneId, $state, $keyword = false)
164
    {
165
        $this->authenticate();
166
        $issues = $this->filterIssuesByState($this->client->api('milestones')->issues($this->issueIntegrator->getProjectName(), $issueMilestoneId), $state);
0 ignored issues
show
Bug introduced by
$this->client->api('mile...e(), $issueMilestoneId) of type string is incompatible with the type array expected by parameter $issues of VersionControl\GitlabIss...::filterIssuesByState(). ( Ignorable by Annotation )

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

166
        $issues = $this->filterIssuesByState(/** @scrutinizer ignore-type */ $this->client->api('milestones')->issues($this->issueIntegrator->getProjectName(), $issueMilestoneId), $state);
Loading history...
167
168
        return $this->mapIssues($issues);
169
    }
170
171
    /**
172
     * @param array  $issues
173
     * @param string $state  open|closed
174
     *
175
     * @return array
176
     */
177
    protected function filterIssuesByState($issues, $state)
178
    {
179
        $filteredIssues = array();
180
        foreach ($issues as $issue) {
181
            if ($issue['state'] === 'opened' && $state === 'open') {
182
                $filteredIssues[] = $issue;
183
            } elseif ($issue['state'] === 'closed' && $state === 'closed') {
184
                $filteredIssues[] = $issue;
185
            }
186
        }
187
188
        return $filteredIssues;
189
    }
190
191
    /**
192
     * @param array $issues
193
     *
194
     * @return array of
195
     */
196
    protected function mapIssues($issues)
197
    {
198
        $issueEntities = array();
199
        if (is_array($issues)) {
0 ignored issues
show
introduced by
The condition is_array($issues) is always true.
Loading history...
200
            foreach ($issues as $issue) {
201
                $issueEntities[] = $this->mapIssueToEntity($issue);
202
            }
203
        }
204
205
        return $issueEntities;
206
    }
207
208
    protected function mapIssueToEntity($issue, $issueComments = array())
209
    {
210
        $issueTransfomer = new IssueToEntityTransformer();
211
        $issueCommentTransfomer = new IssueCommentToEntityTransformer();
212
        $issueEntity = $issueTransfomer->transform($issue);
213
214
        foreach ($issueComments as $issueComment) {
215
            $issueCommentEntity = $issueCommentTransfomer->transform($issueComment);
216
            $issueEntity->addIssueComment($issueCommentEntity);
217
        }
218
219
        return $issueEntity;
220
    }
221
222
    protected function mapEntityToIssue($issueEntity)
223
    {
224
        $issueTransfomer = new IssueToEntityTransformer();
225
226
        return $issueTransfomer->reverseTransform($issueEntity);
227
    }
228
229
    public function newIssueComment()
230
    {
231
        $issueCommentEntity = new IssueComment();
232
233
        return $issueCommentEntity;
234
    }
235
236
    /**
237
     * Creates a New issue Comment on github.
238
     *
239
     * @param \VersionControl\GitlabIssueBundle\Entity\Issues\IssueComment $issueCommentEntity
240
     */
241
    public function createIssueComment(\VersionControl\GitlabIssueBundle\Entity\Issues\IssueComment $issueCommentEntity)
242
    {
243
        $this->authenticate();
244
        $issueId = $issueCommentEntity->getIssue()->getId();
245
        $comment = $this->client->api('issues')->addComment($this->issueIntegrator->getProjectName(), $issueId, $issueCommentEntity->getComment());
246
        $issueCommentTransfomer = new IssueCommentToEntityTransformer();
247
248
        return $issueCommentTransfomer->transform($comment);
0 ignored issues
show
Bug introduced by
$comment of type string is incompatible with the type VersionControl\GitlabIss...ssues\IssueComment|null expected by parameter $issueComment of VersionControl\GitlabIss...ransformer::transform(). ( Ignorable by Annotation )

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

248
        return $issueCommentTransfomer->transform(/** @scrutinizer ignore-type */ $comment);
Loading history...
249
    }
250
}
251