Failed Conditions
Push — master ( cbed7a...eb0d0f )
by Michel
03:05 queued 10s
created

Api::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     * @return array|BaseApi\Result|false
28
     * @throws \Exception
29
     */
30 2
    public function addWorkLogEntry(
31
        string $issueID,
32
        int $seconds,
33
        string $accountId,
34
        string $comment,
35
        string $created
36
    ) {
37
        $params = [
38 2
            'timeSpentSeconds' => $seconds,
39
            'author' => [
40 2
                'accountId' => $accountId,
41
            ],
42 2
            'comment' => $comment,
43 2
            'started' => $created,
44
        ];
45
46 2
        $worklogResponse = $this->api(self::REQUEST_GET, "/rest/api/2/issue/{$issueID}/worklog");
47 2
        $workLogResult = $worklogResponse->getResult();
48
49 2
        $startedDay = (new \DateTimeImmutable($params['started']))->format('d-m-Y');
50
51 2
        if (isset($workLogResult['worklogs'])) {
52 1
            foreach ($workLogResult['worklogs'] as $workLog) {
53 1
                $workLogStartedDay = (new \DateTimeImmutable($workLog['started']))->format('d-m-Y');
54
55 1
                if ($startedDay === $workLogStartedDay &&
56 1
                    $workLog['author']['accountId'] === $accountId
57
                ) {
58 1
                    $params = ['timeSpentSeconds' => $seconds];
59 1
                    return $this->api(self::REQUEST_PUT, "/rest/api/2/issue/{$issueID}/worklog/{$workLog['id']}?adjustEstimate=auto", $params);
60
                }
61
            }
62
        }
63
64 1
        $result = $this->api(self::REQUEST_POST, "/rest/api/2/issue/{$issueID}/worklog?adjustEstimate=auto", $params);
65
66 1
        return $result;
67
    }
68
}
69