Passed
Push — master ( 58efcc...b174ce )
by Michel
01:02 queued 11s
created

Api::addWorkLogEntry()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 51
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8.1419

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 51
rs 8.4444
ccs 20
cts 23
cp 0.8696
cc 8
nc 6
nop 7
crap 8.1419

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace TogglJira\Jira;
5
6
use chobie\Jira\Api as BaseApi;
7
use Exception;
8
9
class Api extends BaseApi
10
{
11
    /**
12
     * @param string $username
13
     * @return array
14
     */
15 1
    public function getUser(string $username): array
16
    {
17 1
        $userDetails = $this->api(self::REQUEST_GET, "/rest/api/2/user", ['username' => $username]);
18
19 1
        return $userDetails->getResult();
20
    }
21
22
    /**
23
     * @param string $issueID
24
     * @param int    $seconds
25
     * @param string $accountId
26
     * @param string $comment
27
     * @param string $created
28
     * @param bool   $overwrite
29
     * @param bool   $notifyUsers
30
     *
31
     * @return array|BaseApi\Result|false
32
     * @throws Exception
33
     */
34 2
    public function addWorkLogEntry(
35
        string $issueID,
36
        int $seconds,
37
        string $accountId,
38
        string $comment,
39
        string $created,
40
        bool $overwrite,
41
        bool $notifyUsers = true
42
    ) {
43 2
        $notify = $notifyUsers ? 'true' : 'false';
44
        $params = [
45 2
            'timeSpentSeconds' => $seconds,
46
            'author' => [
47 2
                'accountId' => $accountId,
48
            ],
49 2
            'comment' => $comment,
50 2
            'started' => $created,
51
        ];
52
53 2
        $worklogResponse = $this->api(self::REQUEST_GET, "/rest/api/2/issue/{$issueID}/worklog");
54 2
        $workLogResult = $worklogResponse->getResult();
55
56 2
        $startedDay = (new \DateTimeImmutable($params['started']))->format('Y-m-d');
57
58 2
        if (isset($workLogResult['worklogs'])) {
59 1
            foreach ($workLogResult['worklogs'] as $workLog) {
60 1
                $workLogStartedDay = (new \DateTimeImmutable($workLog['started']))->format('Y-m-d');
61
62 1
                if ($startedDay !== $workLogStartedDay ||
63 1
                    $workLog['author']['accountId'] !== $accountId) {
64
                    continue;
65
                }
66
67 1
                if (!$overwrite) {
68 1
                    return $this->api(
69 1
                        self::REQUEST_PUT,
70 1
                        "/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}?adjustEstimate=auto&notifyUsers={$notify}",
71 1
                        $params
72
                    );
73
                }
74
75
                if ($overwrite) {
76
                    /**
77
                     * When overwriting the worklogs, delete the existing worklogs first before recreating.
78
                     */
79
                    $this->api(self::REQUEST_DELETE, "/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}");
80
                }
81
            }
82
        }
83
84 1
        return $this->api(self::REQUEST_POST, "/rest/api/2/issue/{$issueID}/worklog?adjustEstimate=auto&notifyUsers={$notify}", $params);
85
    }
86
}
87