Passed
Pull Request — master (#6)
by Timon
07:27
created

Api::addWorkLogEntry()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 48
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.0038

Importance

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