Passed
Pull Request — master (#27)
by Laurens
02:00
created

KvkClientFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 12
dl 0
loc 49
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 10 1
A createHttpClient() 0 24 2
A createProfileResponseFactory() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\KvkApi;
6
7
use GuzzleHttp\Client;
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\KvkPaginatorFactory;
13
use Werkspot\KvkApi\Client\Factory\Profile\Company\AddressFactory;
14
use Werkspot\KvkApi\Client\Factory\Profile\Company\BusinessActivityFactory;
15
use Werkspot\KvkApi\Client\Factory\Profile\Company\TradeNamesFactory;
16
use Werkspot\KvkApi\Client\Factory\Profile\CompanyFactory;
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 34
    public static function create(
24
        string $userKey,
25
        MapperInterface $endpoint,
26
        ?string $rootCertificate = null
27
    ): KvkClient {
28 34
        return new KvkClient(
29 34
            self::createHttpClient($userKey, $endpoint, $rootCertificate),
30 34
            self::createProfileResponseFactory()
31
        );
32
    }
33
34 34
    private static function createHttpClient(
35
        string $userKey,
36
        MapperInterface $endpoint,
37
        ?string $rootCertificate = null
38
    ): ClientInterface {
39 34
        $stack = HandlerStack::create();
40 34
        $stack->unshift(Middleware::mapRequest(function (RequestInterface $request) use ($userKey) {
41 34
            return $request->withUri(Uri::withQueryValue($request->getUri(), 'user_key', $userKey));
42 34
        }));
43
44 34
        if ($rootCertificate === null) {
45
            trigger_error('kvk-api: Not using a root certificate is deprecated and will be required in version 1.0. Please configure a root certificate.', E_USER_DEPRECATED);
46
        }
47
48 34
        $client = new Client([
49 34
            'verify' => $rootCertificate ?? false,
50 34
            'handler' => $stack,
51
        ]);
52
53 34
        return new GuzzleClient(
54 34
            $client,
55 34
            $endpoint
56
        );
57
    }
58
59 34
    private static function createProfileResponseFactory(): KvkPaginatorFactory
60
    {
61 34
        return new KvkPaginatorFactory(
62 34
            new CompanyFactory(
63 34
                new TradeNamesFactory(),
64 34
                new BusinessActivityFactory(),
65 34
                new AddressFactory()
66
            )
67
        );
68
    }
69
}
70