LoggingClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 36
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A sendRequest() 0 20 3
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)));
0 ignored issues
show
Bug introduced by
The method debug() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
		$this->logger->/** @scrutinizer ignore-call */ 
43
                 debug(sprintf("\n----HTTP-REQUEST----\n%s", MessageUtil::toString($request)));

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.

Loading history...
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