Passed
Pull Request — master (#27)
by Laurens
01:49
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\HandlerStack;
8
use GuzzleHttp\Middleware;
9
use GuzzleHttp\Psr7\Uri;
10
use Psr\Http\Message\RequestInterface;
11
use Werkspot\KvkApi\Client\Factory\KvkPaginatorFactory;
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\Http\Adapter\Guzzle\Client as GuzzleClient;
17
use Werkspot\KvkApi\Http\ClientInterface;
18
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
19
20
final class KvkClientFactory
21
{
22 34
    public static function create(
23
        string $userKey,
24
        MapperInterface $endpoint,
25
        ?string $rootCertificate = null
26
    ): KvkClient {
27 34
        return new KvkClient(
28 34
            self::createHttpClient($userKey, $endpoint, $rootCertificate),
29 34
            self::createProfileResponseFactory()
30
        );
31
    }
32
33 34
    private static function createHttpClient(
34
        string $userKey,
35
        MapperInterface $endpoint,
36
        ?string $rootCertificate = null
37
    ): ClientInterface {
38 34
        $stack = HandlerStack::create();
39 34
        $stack->unshift(Middleware::mapRequest(function (RequestInterface $request) use ($userKey) {
40 34
            return $request->withUri(Uri::withQueryValue($request->getUri(), 'user_key', $userKey));
41 34
        }));
42
43 34
        if ($rootCertificate === null) {
44
            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);
45
        }
46
47 34
        $client = new \GuzzleHttp\Client([
48 34
            'verify' => $rootCertificate ?? false,
49 34
            'handler' => $stack,
50
        ]);
51
52 34
        return new GuzzleClient(
53 34
            $client,
54 34
            $endpoint
55
        );
56
    }
57
58 34
    private static function createProfileResponseFactory(): KvkPaginatorFactory
59
    {
60 34
        return new KvkPaginatorFactory(
61 34
            new CompanyFactory(
62 34
                new TradeNamesFactory(),
63 34
                new BusinessActivityFactory(),
64 34
                new AddressFactory()
65
            )
66
        );
67
    }
68
}
69