|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Werkspot\KvkApi; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
|
8
|
|
|
use GuzzleHttp\HandlerStack; |
|
9
|
|
|
use GuzzleHttp\Middleware; |
|
10
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
11
|
|
|
use Psr\Http\Message\RequestInterface; |
|
12
|
|
|
use Werkspot\KvkApi\Client\Factory\Profile\Company\AddressFactory; |
|
13
|
|
|
use Werkspot\KvkApi\Client\Factory\Profile\Company\BusinessActivityFactory; |
|
14
|
|
|
use Werkspot\KvkApi\Client\Factory\Profile\Company\TradeNamesFactory; |
|
15
|
|
|
use Werkspot\KvkApi\Client\Factory\Profile\CompanyFactory; |
|
16
|
|
|
use Werkspot\KvkApi\Client\Factory\ProfileResponseFactory; |
|
17
|
|
|
use Werkspot\KvkApi\Http\Adapter\Guzzle\Client as GuzzleClient; |
|
18
|
|
|
use Werkspot\KvkApi\Http\ClientInterface; |
|
19
|
|
|
use Werkspot\KvkApi\Http\Endpoint\MapperInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class KvkClientFactory |
|
22
|
|
|
{ |
|
23
|
18 |
|
public static function create(string $userKey, MapperInterface $endpoint): KvkClient |
|
24
|
|
|
{ |
|
25
|
18 |
|
return new KvkClient( |
|
26
|
18 |
|
self::createHttpClient($userKey, $endpoint), |
|
27
|
18 |
|
self::createProfileResponseFactory() |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
18 |
|
private static function createHttpClient(string $userKey, MapperInterface $endpoint): ClientInterface |
|
32
|
|
|
{ |
|
33
|
18 |
|
$stack = HandlerStack::create(); |
|
34
|
18 |
|
$stack->unshift(Middleware::mapRequest(function (RequestInterface $request) use ($userKey) { |
|
35
|
18 |
|
return $request->withUri(Uri::withQueryValue($request->getUri(), 'user_key', $userKey)); |
|
36
|
18 |
|
})); |
|
37
|
|
|
|
|
38
|
18 |
|
return new GuzzleClient( |
|
39
|
18 |
|
new \GuzzleHttp\Client(['handler' => $stack]), |
|
40
|
18 |
|
$endpoint |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
18 |
|
private static function createProfileResponseFactory(): ProfileResponseFactory |
|
45
|
|
|
{ |
|
46
|
18 |
|
return new ProfileResponseFactory( |
|
47
|
18 |
|
new CompanyFactory( |
|
48
|
18 |
|
new TradeNamesFactory(), |
|
49
|
18 |
|
new BusinessActivityFactory(), |
|
50
|
18 |
|
new AddressFactory() |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|