Passed
Push — master ( 63d24c...8783ea )
by Michel
02:45 queued 10s
created

Api   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
wmc 8
eloc 26
dl 0
loc 72
ccs 22
cts 25
cp 0.88
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 5 1
B addWorkLogEntry() 0 49 7
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
     * @throws \Exception
30
     */
31 2
    public function addWorkLogEntry(
32
        string $issueID,
33
        int $seconds,
34
        string $accountId,
35
        string $comment,
36
        string $created,
37
        bool $overwrite
38
    ) {
39
        $params = [
40 2
            'timeSpentSeconds' => $seconds,
41
            'author' => [
42 2
                'accountId' => $accountId,
43
            ],
44 2
            'comment' => $comment,
45 2
            'started' => $created,
46
        ];
47
48 2
        $worklogResponse = $this->api(self::REQUEST_GET, "/rest/api/2/issue/{$issueID}/worklog");
49 2
        $workLogResult = $worklogResponse->getResult();
50
51 2
        $startedDay = (new \DateTimeImmutable($params['started']))->format('Y-m-d');
52
53 2
        if (isset($workLogResult['worklogs'])) {
54 1
            foreach ($workLogResult['worklogs'] as $workLog) {
55 1
                $workLogStartedDay = (new \DateTimeImmutable($workLog['started']))->format('Y-m-d');
56
57 1
                if ($startedDay !== $workLogStartedDay ||
58 1
                    $workLog['author']['accountId'] !== $accountId) {
59
                    continue;
60
                }
61
62 1
                if (!$overwrite) {
63 1
                    return $this->api(
64 1
                        self::REQUEST_PUT,
65 1
                        "/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}?adjustEstimate=auto",
66 1
                        $params
67
                    );
68
                }
69
70
                if ($overwrite) {
71
                    /**
72
                     * When overwriting the worklogs, delete the existing worklogs first before recreating.
73
                     */
74
                    $this->api(self::REQUEST_DELETE, "/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}");
75
                }
76
            }
77
        }
78
79 1
        return $this->api(self::REQUEST_POST, "/rest/api/2/issue/{$issueID}/worklog?adjustEstimate=auto", $params);
80
    }
81
}
82