1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Syncer\Toggl; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use JMS\Serializer\SerializerInterface; |
7
|
|
|
use Syncer\Dto\Toggl\Workspace; |
8
|
|
|
use Syncer\Dto\Toggl\Client; |
9
|
|
|
/** |
10
|
|
|
* Class TogglClient |
11
|
|
|
* @package Syncer\Toggl |
12
|
|
|
* |
13
|
|
|
* @author Matthieu Calie <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class TogglClient |
16
|
|
|
{ |
17
|
|
|
const VERSION = 'v8'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Client; |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SerializerInterface |
26
|
|
|
*/ |
27
|
|
|
private $serializer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $api_key; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* TogglClient constructor. |
36
|
|
|
* @param Client $client |
37
|
|
|
* @param SerializerInterface $serializer |
38
|
|
|
* @param $api_key |
39
|
|
|
*/ |
40
|
2 |
|
public function __construct(GuzzleClient $client, SerializerInterface $serializer, $api_key) |
41
|
|
|
{ |
42
|
2 |
|
$this->client = $client; |
43
|
2 |
|
$this->serializer = $serializer; |
44
|
2 |
|
$this->api_key = $api_key; |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array|Workspace[] |
49
|
|
|
*/ |
50
|
1 |
|
public function getWorkspaces() |
51
|
|
|
{ |
52
|
1 |
|
$response = $this->client->request('GET', self::VERSION . '/workspaces', [ |
53
|
1 |
|
'auth' => [$this->api_key, 'api_token'], |
54
|
|
|
]); |
55
|
|
|
|
56
|
1 |
|
return $this->serializer->deserialize($response->getBody(), 'array<Syncer\Dto\Toggl\Workspace>', 'json'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array|Client[] |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function getClientsForWorkspace($workspaceId) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$response = $this->client->request('GET', self::VERSION . '/workspaces/' . $workspaceId . '/clients', [ |
65
|
|
|
'auth' => [$this->api_key, 'api_token'], |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
return $this->serializer->deserialize($response->getBody(), 'array<Syncer\Dto\Toggl\Client>', 'json'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array|Project[] |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function getProjectsForWorkspace($workspaceId) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$response = $this->client->request('GET', self::VERSION . '/workspaces/' . $workspaceId . '/projects', [ |
77
|
|
|
'auth' => [$this->api_key, 'api_token'], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
return $this->serializer->deserialize($response->getBody(), 'array<Syncer\Dto\Toggl\Project>', 'json'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.