|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JumpCloud\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Psr7\Request as HttpRequest; |
|
8
|
|
|
use GuzzleHttp\Psr7\Response; |
|
9
|
|
|
use JumpCloud\Request\RequestInterface; |
|
10
|
|
|
use JumpCloud\Response\ResponseInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Psr\Log\NullLogger; |
|
13
|
|
|
use Webmozart\Json\JsonEncoder; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class RequestHandler |
|
17
|
|
|
* |
|
18
|
|
|
* @package JumpCloud |
|
19
|
|
|
*/ |
|
20
|
|
|
class RequestHandler |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var NullLogger |
|
24
|
|
|
*/ |
|
25
|
|
|
private $logger; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var JsonEncoder |
|
29
|
|
|
*/ |
|
30
|
|
|
private $jsonEncoder; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* RequestHandler constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param LoggerInterface|null $logger |
|
36
|
|
|
*/ |
|
37
|
15 |
|
public function __construct(LoggerInterface $logger = null) |
|
38
|
|
|
{ |
|
39
|
15 |
|
$this->logger = $logger ?: new NullLogger(); |
|
|
|
|
|
|
40
|
15 |
|
$this->jsonEncoder = new JsonEncoder(); |
|
41
|
15 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param GuzzleClient $client |
|
45
|
|
|
* @param RequestInterface $request |
|
46
|
|
|
* @return ResponseInterface |
|
47
|
|
|
*/ |
|
48
|
13 |
|
public function handle(GuzzleClient $client, RequestInterface $request) |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
13 |
|
$response = $client->send( |
|
52
|
13 |
|
new HttpRequest( |
|
53
|
13 |
|
$request->getMethod(), |
|
54
|
12 |
|
$request->getUri(), |
|
55
|
12 |
|
$request->getHeaders(), |
|
56
|
12 |
|
$this->jsonEncoder->encode($request->getBody()) |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
7 |
|
} catch (\Exception $e) { |
|
60
|
7 |
|
$this->logger->debug( |
|
61
|
7 |
|
sprintf('Exception thrown during Guzzle request'), |
|
62
|
|
|
[ |
|
63
|
7 |
|
'message' => $e->getMessage(), |
|
64
|
7 |
|
'line' => $e->getLine(), |
|
65
|
7 |
|
'file' => $e->getFile(), |
|
66
|
7 |
|
'trace' => $e->getTraceAsString() |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
7 |
|
$response = new Response(500); |
|
71
|
|
|
|
|
72
|
7 |
|
if ($e instanceof ClientException) { |
|
73
|
2 |
|
$response = new Response(401); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
13 |
|
return $request->getResponseFactory()->create($response); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.