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

Api   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 7
eloc 25
dl 0
loc 70
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 5 1
B addWorkLogEntry() 0 48 6
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