1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JDecool\Clockify; |
6
|
|
|
|
7
|
|
|
use Http\Client\{ |
8
|
|
|
Common\HttpMethodsClient, |
9
|
|
|
Common\Plugin\AddHostPlugin, |
10
|
|
|
Common\Plugin\AddPathPlugin, |
11
|
|
|
Common\Plugin\AuthenticationPlugin, |
12
|
|
|
Common\Plugin\HeaderSetPlugin, |
13
|
|
|
Common\PluginClient, |
14
|
|
|
HttpClient, |
15
|
|
|
}; |
16
|
|
|
use Http\Message\Authentication\Header; |
17
|
|
|
use Psr\Http\Message\{ |
18
|
|
|
RequestFactoryInterface, |
19
|
|
|
StreamFactoryInterface, |
20
|
|
|
UriFactoryInterface, |
21
|
|
|
}; |
22
|
|
|
use Http\Discovery\{ |
23
|
|
|
Psr17FactoryDiscovery, |
24
|
|
|
Psr18ClientDiscovery, |
25
|
|
|
}; |
26
|
|
|
use RuntimeException; |
27
|
|
|
|
28
|
|
|
class ClientBuilder |
29
|
|
|
{ |
30
|
|
|
private const ENDPOINT_V1 = 'https://api.clockify.me/api/v1/'; |
31
|
|
|
|
32
|
|
|
private $httpClient; |
33
|
|
|
private $requestFactory; |
34
|
|
|
private $uriFactory; |
35
|
|
|
private $streamFactory; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
?HttpClient $httpClient = null, |
39
|
|
|
?RequestFactoryInterface $requestFactory = null, |
40
|
|
|
?UriFactoryInterface $uriFactory = null, |
41
|
|
|
?StreamFactoryInterface $streamFactory = null |
42
|
|
|
) { |
43
|
|
|
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find(); |
44
|
|
|
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); |
45
|
|
|
$this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); |
46
|
|
|
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function createClientV1(string $apiKey): Client |
50
|
|
|
{ |
51
|
|
|
return $this->create(self::ENDPOINT_V1, $apiKey); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function create(string $endpoint, string $apiKey): Client |
55
|
|
|
{ |
56
|
|
|
if (false === filter_var($endpoint, FILTER_VALIDATE_URL)) { |
57
|
|
|
throw new RuntimeException('Invalid Clockify endpoint.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ('' === trim($apiKey)) { |
61
|
|
|
throw new RuntimeException('API token is required.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$plugins = [ |
65
|
|
|
new AuthenticationPlugin( |
66
|
|
|
new Header('X-Api-Key', $apiKey), |
67
|
|
|
), |
68
|
|
|
new AddHostPlugin( |
69
|
|
|
$this->uriFactory->createUri($endpoint), |
70
|
|
|
), |
71
|
|
|
new AddPathPlugin( |
72
|
|
|
$this->uriFactory->createUri($endpoint), |
73
|
|
|
), |
74
|
|
|
new HeaderSetPlugin([ |
75
|
|
|
'User-Agent' => 'github.com/jdecool/clockify-api', |
76
|
|
|
'Content-Type' => 'application/json', |
77
|
|
|
]), |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$http = new HttpMethodsClient( |
81
|
|
|
new PluginClient($this->httpClient, $plugins), |
82
|
|
|
$this->requestFactory, |
83
|
|
|
$this->streamFactory, |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
return new Client($http); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|