1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class LoggingClient |
4
|
|
|
* |
5
|
|
|
* a silly logging wrapper (do not use in production!) |
6
|
|
|
* |
7
|
|
|
* @created 07.08.2019 |
8
|
|
|
* @author smiley <[email protected]> |
9
|
|
|
* @copyright 2019 smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\HTTP\Psr18; |
14
|
|
|
|
15
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
16
|
|
|
use Psr\Http\Client\{ClientExceptionInterface, ClientInterface}; |
17
|
|
|
use Psr\Http\Message\{RequestInterface, ResponseInterface}; |
18
|
|
|
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger}; |
19
|
|
|
use Throwable; |
20
|
|
|
use function get_class, sprintf; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @codeCoverageIgnore |
24
|
|
|
*/ |
25
|
|
|
class LoggingClient implements ClientInterface, LoggerAwareInterface{ |
26
|
|
|
use LoggerAwareTrait; |
27
|
|
|
|
28
|
|
|
protected ClientInterface $http; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* LoggingClient constructor. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ClientInterface $http, LoggerInterface $logger = null){ |
34
|
|
|
$this->http = $http; |
35
|
|
|
$this->logger = $logger ?? new NullLogger; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
public function sendRequest(RequestInterface $request):ResponseInterface{ |
42
|
|
|
$this->logger->debug(sprintf("\n----HTTP-REQUEST----\n%s", MessageUtil::toString($request))); |
|
|
|
|
43
|
|
|
|
44
|
|
|
try{ |
45
|
|
|
$response = $this->http->sendRequest($request); |
46
|
|
|
|
47
|
|
|
$this->logger->debug(sprintf("\n----HTTP-RESPONSE---\n%s", MessageUtil::toString($response))); |
48
|
|
|
} |
49
|
|
|
catch(Throwable $e){ |
50
|
|
|
$this->logger->error($e->getMessage()); |
51
|
|
|
$this->logger->error($e->getTraceAsString()); |
52
|
|
|
|
53
|
|
|
if(!$e instanceof ClientExceptionInterface){ |
54
|
|
|
throw new ClientException(sprintf('unexpected exception, does not implement "ClientExceptionInterface": %s', get_class($e))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw $e; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $response; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.