Passed
Pull Request — master (#27)
by Laurens
01:49
created

KvkClientFactory::createProfileResponseFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
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