Guzzle dependency stopped the webmentions job being serialized

This commit is contained in:
Jonny Barnes 2016-06-23 17:32:50 +01:00
parent 3a7f8fb647
commit 2be6a4ec4b

View file

@ -13,36 +13,34 @@ class SendWebMentions extends Job implements ShouldQueue
use InteractsWithQueue, SerializesModels; use InteractsWithQueue, SerializesModels;
protected $note; protected $note;
protected $guzzle;
/** /**
* Create the job instance, inject dependencies. * Create the job instance, inject dependencies.
* *
* @param User $user
* @param Note $note * @param Note $note
* @return void * @return void
*/ */
public function __construct(Note $note, Client $guzzle = null) public function __construct(Note $note)
{ {
$this->note = $note; $this->note = $note;
$this->guzzle = $guzzle ?? new Client();
} }
/** /**
* Execute the job. * Execute the job.
* *
* @param \GuzzleHttp\Client $guzzle
* @return void * @return void
*/ */
public function handle(Note $note) public function handle(Client $guzzle)
{ {
//grab the URLs //grab the URLs
$urlsInReplyTo = explode(' ', $this->note->in_reply_to); $urlsInReplyTo = explode(' ', $this->note->in_reply_to);
$urlsNote = $this->getLinks($this->note->note); $urlsNote = $this->getLinks($this->note->note);
$urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs $urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
foreach ($urls as $url) { foreach ($urls as $url) {
$endpoint = $this->discoverWebmentionEndpoint($url); $endpoint = $this->discoverWebmentionEndpoint($url, $guzzle);
if ($endpoint) { if ($endpoint) {
$this->guzzle->post($endpoint, [ $guzzle->post($endpoint, [
'form_params' => [ 'form_params' => [
'source' => $this->note->longurl, 'source' => $this->note->longurl,
'target' => $url, 'target' => $url,
@ -56,10 +54,10 @@ class SendWebMentions extends Job implements ShouldQueue
* Discover if a URL has a webmention endpoint. * Discover if a URL has a webmention endpoint.
* *
* @param string The URL * @param string The URL
* @param \GuzzleHttp\Client $client * @param \GuzzleHttp\Client $guzzle
* @return string The webmention endpoint URL * @return string The webmention endpoint URL
*/ */
private function discoverWebmentionEndpoint($url) private function discoverWebmentionEndpoint($url, $guzzle)
{ {
//lets not send webmentions to myself //lets not send webmentions to myself
if (parse_url($url, PHP_URL_HOST) == env('LONG_URL', 'localhost')) { if (parse_url($url, PHP_URL_HOST) == env('LONG_URL', 'localhost')) {
@ -68,7 +66,7 @@ class SendWebMentions extends Job implements ShouldQueue
$endpoint = null; $endpoint = null;
$response = $this->guzzle->get($url); $response = $guzzle->get($url);
//check HTTP Headers for webmention endpoint //check HTTP Headers for webmention endpoint
$links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link')); $links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
foreach ($links as $link) { foreach ($links as $link) {
@ -100,6 +98,9 @@ class SendWebMentions extends Job implements ShouldQueue
/** /**
* Get the URLs from a note. * Get the URLs from a note.
*
* @param string $html
* @return array $urls
*/ */
private function getLinks($html) private function getLinks($html)
{ {