HttpClientFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 11
dl 0
loc 90
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstallationClient() 0 12 1
B create() 0 31 1
A createBaseClient() 0 15 1
1
<?php
2
3
namespace Bunq;
4
5
use Bunq\Certificate\CertificateType;
6
use Bunq\Certificate\Storage\CertificateStorage;
7
use Bunq\Middleware\RefreshSessionMiddleware;
8
use Bunq\Middleware\RequestAuthenticationMiddleware;
9
use Bunq\Middleware\RequestIdMiddleware;
10
use Bunq\Middleware\RequestSignatureMiddleware;
11
use Bunq\Middleware\ResponseSignatureMiddleware;
12
use Bunq\Service\InstallationService;
13
use Bunq\Service\TokenService;
14
use Bunq\Token\Storage\TokenStorage;
15
use GuzzleHttp\ClientInterface;
16
use GuzzleHttp\HandlerStack;
17
use GuzzleHttp\Middleware;
18
19
final class HttpClientFactory
20
{
21
    /**
22
     * Creates an installation client
23
     *
24
     * @param string             $url
25
     * @param CertificateStorage $certificateStorage
26
     *
27
     * @return ClientInterface
28
     */
29
    public static function createInstallationClient($url, CertificateStorage $certificateStorage)
30
    {
31
        $handlerStack = HandlerStack::create();
32
        $handlerStack->push(
33
            Middleware::mapRequest(new RequestIdMiddleware())
34
        );
35
        $handlerStack->push(
36
            Middleware::mapRequest(new RequestSignatureMiddleware($certificateStorage->load(CertificateType::PRIVATE_KEY())))
37
        );
38
39
        return self::createBaseClient((string)$url, $handlerStack);
40
    }
41
42
    /**
43
     * Creates the HttpClient with all handlers
44
     *
45
     * @param string              $url
46
     * @param TokenService        $tokenService
47
     * @param CertificateStorage  $certificateStorage
48
     * @param InstallationService $installationService
49
     * @param TokenStorage        $tokenStorage
50
     *
51
     * @return ClientInterface
52
     */
53
    public static function create(
54
        $url,
55
        TokenService $tokenService,
56
        CertificateStorage $certificateStorage,
57
        InstallationService $installationService,
58
        TokenStorage $tokenStorage
59
    ) {
60
        $sessionToken    = $tokenService->sessionToken();
61
        $publicServerKey = $certificateStorage->load(CertificateType::BUNQ_SERVER_KEY());
62
63
        $handlerStack = HandlerStack::create();
64
        $handlerStack->push(
65
            Middleware::mapRequest(new RequestIdMiddleware())
66
        );
67
        $handlerStack->push(
68
            Middleware::mapRequest(new RequestAuthenticationMiddleware($sessionToken))
69
        );
70
        $handlerStack->push(
71
            Middleware::mapRequest(new RequestSignatureMiddleware($certificateStorage->load(CertificateType::PRIVATE_KEY())))
72
        );
73
        $handlerStack->push(
74
            Middleware::mapResponse(new ResponseSignatureMiddleware($publicServerKey))
75
        );
76
        $handlerStack->push(
77
            Middleware::mapResponse(new RefreshSessionMiddleware($installationService, $tokenStorage))
78
        );
79
80
        $httpClient = self::createBaseClient($url, $handlerStack);
81
82
        return $httpClient;
83
    }
84
85
    /**
86
     * Returns the standard used headers.
87
     *
88
     * @param string            $url
89
     * @param HandlerStack|null $handlerStack
90
     *
91
     * @return ClientInterface
92
     */
93
    private static function createBaseClient($url, HandlerStack $handlerStack = null)
94
    {
95
        $httpClient = new \GuzzleHttp\Client(
96
            [
97
                'base_uri' => $url,
98
                'handler'  => $handlerStack,
99
                'headers'  => [
100
                    'Content-Type' => 'application/json',
101
                    'User-Agent'   => 'bunq-api-client:user',
102
                ],
103
            ]
104
        );
105
106
        return $httpClient;
107
    }
108
}
109