Test Failed
Pull Request — master (#4)
by
unknown
01:25
created

ReportsClient::getDetailedReport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Syncer\Toggl;
4
5
use Carbon\Carbon;
6
use GuzzleHttp\Client;
7
use JMS\Serializer\SerializerInterface;
8
use Syncer\Dto\Toggl\DetailedReport;
9
10
/**
11
 * Class ReportsClient
12
 * @package Syncer\Toggl
13
 *
14
 * @author Matthieu Calie <[email protected]>
15
 */
16
class ReportsClient
17
{
18
    const VERSION = 'v2';
19
20
    /**
21
     * @var Client;
22
     */
23
    private $client;
24
25
    /**
26
     * @var SerializerInterface
27
     */
28
    private $serializer;
29
30
    /**
31
     * @var string
32
     */
33
    private $api_key;
34
35
    /**
36
     * TogglClient constructor.
37
     * @param Client $client
38
     * @param SerializerInterface $serializer
39
     * @param $api_key
40
     */
41 2
    public function __construct(Client $client, SerializerInterface $serializer, $api_key)
42
    {
43 2
        $this->client = $client;
44 2
        $this->serializer = $serializer;
45 2
        $this->api_key = $api_key;
46 2
    }
47
48
    /**
49
     * Get detailed report from since provided numbers of days ago
50
     *
51
     * @param int $workspaceId
52
     * @param int $sinceDaysAgo
53
     * @return array|\JMS\Serializer\scalar|object|DetailedReport
54
     */
55
    public function getDetailedReport(int $workspaceId, int $sinceDaysAgo)
56
    {
57
        $res = $this->client->request('GET', self::VERSION . '/details', [
58
            'auth' => [$this->api_key, 'api_token'],
59
            'query' => [
60
                'user_agent' => '[email protected]',
61
                'workspace_id' => $workspaceId,
62
                'since' => Carbon::now()->subDays($sinceDaysAgo)->format('Y-m-d')
63
            ]
64
        ]);
65
66
        return $this->serializer->deserialize($res->getBody(), DetailedReport::class, 'json');
67
    }
68
}
69