Skip to content

Commit f407fd3

Browse files
author
Younès El Biache
committed
Guzzle Resolver added
1 parent d53edd0 commit f407fd3

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/RequestResolvers/Guzzle.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Embed\RequestResolvers;
4+
5+
use GuzzleHttp\Client;
6+
7+
class Guzzle implements RequestResolverInterface
8+
{
9+
/**
10+
* @var Client
11+
*/
12+
protected $client;
13+
14+
/**
15+
* @var GuzzleHttp\Request
16+
*/
17+
protected $request;
18+
19+
/**
20+
* @var GuzzleHttp\Response
21+
*/
22+
protected $response;
23+
24+
/**
25+
* Constructor. Sets the url
26+
*
27+
* @param string $url The url value
28+
* @param array $config The resolver configuration
29+
*/
30+
public function __construct($url, array $config)
31+
{
32+
$this->client = new Client($config ?: []);
33+
$this->request = $this->client->createRequest('GET', $url);
34+
}
35+
36+
/**
37+
* Get the http code of the url, for example: 200
38+
*
39+
* @return int The http code
40+
*/
41+
public function getHttpCode()
42+
{
43+
return $this->getResponse()->getStatusCode();
44+
}
45+
46+
/**
47+
* Get the content-type of the url, for example: text/html
48+
*
49+
* @return string The content-type header or null
50+
*/
51+
public function getMimeType()
52+
{
53+
return $this->getResponse()->getHeader('Content-Type');
54+
}
55+
56+
/**
57+
* Get the content of the url
58+
*
59+
* @return string The content or false
60+
*/
61+
public function getContent()
62+
{
63+
return $this->getResponse()->getBody()->getContents();
64+
}
65+
66+
/**
67+
* Return the final url (after all possible redirects)
68+
*
69+
* @return string The final url
70+
*/
71+
public function getUrl()
72+
{
73+
return $this->getResponse()->getEffectiveUrl();
74+
}
75+
76+
/**
77+
* Return the http request info (for debug purposes)
78+
*
79+
* @return array
80+
*/
81+
public function getRequestInfo()
82+
{
83+
return $this->request->getConfig();
84+
}
85+
86+
/**
87+
* Get the result of the http request
88+
*
89+
* @return Response
90+
*/
91+
protected function getResponse()
92+
{
93+
if ($this->response === null) {
94+
$this->response = $this->client->send($this->request);
95+
}
96+
97+
return $this->response;
98+
}
99+
}

0 commit comments

Comments
 (0)