VerboseClient::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Http\Client;
17
18
use GuzzleHttp\ClientInterface;
19
use GuzzleHttp\HandlerStack;
20
use GuzzleHttp\Middleware;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
25
/**
26
 * Http client that logs every request and response
27
 */
28
class VerboseClient implements ClientInterface
29
{
30
    /**
31
     * Logger as it is
32
     *
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * Converts an object into a set of arrays/scalars
39
     *
40
     * @var NormalizerInterface
41
     */
42
    private $normalizer;
43
44
    /**
45
     * Base http client implementation
46
     *
47
     * @var ClientInterface
48
     */
49
    private $httpClient;
50
51
    /**
52
     * VerboseClient constructor.
53
     *
54
     * @param LoggerInterface     $logger     Logger as it is
55
     * @param NormalizerInterface $normalizer Converts an object into a set of arrays/scalars
56
     * @param ClientInterface     $httpClient Base http client implementation
57
     */
58
    public function __construct(LoggerInterface $logger, NormalizerInterface $normalizer, ClientInterface $httpClient)
59
    {
60
        $this->logger     = $logger;
61
        $this->normalizer = $normalizer;
62
        $this->httpClient = $httpClient;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function send(RequestInterface $request, array $options = [])
69
    {
70
        return $this->httpClient->send($request, $options);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function sendAsync(RequestInterface $request, array $options = [])
77
    {
78
        return $this->httpClient->sendAsync($request, $options);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function request($method, $uri, array $options = [])
85
    {
86
        $transactions = [];
87
        $history      = Middleware::history($transactions);
88
89
        $handlerStack = HandlerStack::create();
90
        $handlerStack->push($history);
91
92
        $verboseOptions = [
93
            'handler' => $handlerStack,
94
        ];
95
96
        $optionsMerged = array_replace_recursive($options, $verboseOptions);
97
98
        $response = $this->httpClient->request($method, $uri, $optionsMerged);
99
100
        $transactionsNormalized = $this->normalizer->normalize($transactions);
101
        $this->logger->info('Http request sent.', ['transactions' => $transactionsNormalized]);
102
103
        $response->getBody()->rewind();
104
105
        return $response;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function requestAsync($method, $uri, array $options = [])
112
    {
113
        return $this->httpClient->requestAsync($method, $uri, $options);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getConfig($option = null)
120
    {
121
        return $this->httpClient->getConfig($option);
122
    }
123
}
124