Skip to content

Commit a9307f0

Browse files
committed
+ Timeouts and errors
1 parent 17ab2f0 commit a9307f0

File tree

8 files changed

+117
-11
lines changed

8 files changed

+117
-11
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ array(4) {
7575
}
7676
```
7777

78+
## Timeouts and errors
79+
80+
```php
81+
// Default connect timeout is 5 seconds, you can change it to anything you want
82+
$client->getParser('general')->getReader()->config(['connect_timeout' => 3.14]);
83+
84+
// Default maximum redirect count is 10, but you can change it too
85+
$client->getParser('general')->getReader()->config(['allow_redirects' => ['max' => 10]]);
86+
87+
// If there is a network error (DNS, connect, etc), we throw ConnectionErrorException
88+
try {
89+
$previews = $previewClient->getPreviews();
90+
} catch (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
91+
echo "Oh no!";
92+
}
93+
```
94+
7895
### YouTube example
7996

8097
```php

src/Client.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public function addParser(ParserInterface $parser)
7474
return $this;
7575
}
7676

77+
/**
78+
* @param $id
79+
* @return bool|ParserInterface
80+
*/
81+
public function getParser($id)
82+
{
83+
return isset($this->parsers[$id]) ? $this->parsers[$id] : false;
84+
}
85+
7786
/**
7887
* Get parsers
7988
* @return ParserInterface[]

src/Contracts/LinkInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,10 @@ public function isHtml();
7575
* @return boolean
7676
*/
7777
public function isImage();
78+
79+
/**
80+
* Is this link functioning? (could we open it?)
81+
* @return boolean
82+
*/
83+
public function isUp();
7884
}

src/Contracts/ReaderInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ interface ReaderInterface
1313
* @return LinkInterface
1414
*/
1515
public function readLink(LinkInterface $link);
16+
17+
/**
18+
* @param array $parameters
19+
* @return void
20+
*/
21+
public function config(array $parameters);
1622
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Dusterio\LinkPreview\Exceptions;
4+
5+
/**
6+
* Class ConnectionErorException
7+
* @package Dusterio\LinkPreview\Exceptions
8+
*/
9+
class ConnectionErrorException extends \Exception
10+
{
11+
12+
}

src/Models/Link.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,12 @@ public function isImage()
129129
{
130130
return !strncmp($this->getContentType(), 'image/', strlen('image/'));
131131
}
132+
133+
/**
134+
* @inheritdoc
135+
*/
136+
public function isUp()
137+
{
138+
return $this->content !== false && $this->contentType !== false;
139+
}
132140
}

src/Parsers/HtmlParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Dusterio\LinkPreview\Contracts\PreviewInterface;
77
use Dusterio\LinkPreview\Contracts\ReaderInterface;
88
use Dusterio\LinkPreview\Contracts\ParserInterface;
9+
use Dusterio\LinkPreview\Exceptions\ConnectionErrorException;
910
use Dusterio\LinkPreview\Models\Link;
1011
use Dusterio\LinkPreview\Readers\HttpReader;
1112
use Dusterio\LinkPreview\Models\HtmlPreview;
@@ -93,6 +94,8 @@ public function parseLink(LinkInterface $link)
9394
{
9495
$link = $this->readLink($link);
9596

97+
if (!$link->isUp()) throw new ConnectionErrorException();
98+
9699
if ($link->isHtml()) {
97100
$this->getPreview()->update($this->parseHtml($link));
98101
} else if ($link->isImage()) {

src/Readers/HttpReader.php

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GuzzleHttp\Client;
88
use GuzzleHttp\Cookie\CookieJar;
99
use GuzzleHttp\TransferStats;
10+
use GuzzleHttp\Exception\ConnectException;
1011

1112
/**
1213
* Class HttpReader
@@ -18,6 +19,49 @@ class HttpReader implements ReaderInterface
1819
*/
1920
private $client;
2021

22+
/**
23+
* @var array $config
24+
*/
25+
private $config;
26+
27+
/**
28+
* @var CookieJar $jar
29+
*/
30+
private $jar;
31+
32+
/**
33+
* HttpReader constructor.
34+
* @param array|null $config
35+
*/
36+
public function __construct($config = null)
37+
{
38+
$this->jar = new CookieJar();
39+
40+
$this->config = $config ?: [
41+
'allow_redirects' => ['max' => 10],
42+
'cookies' => $this->jar,
43+
'connect_timeout' => 5
44+
];
45+
}
46+
47+
/**
48+
* @param int $timeout
49+
*/
50+
public function setTimeout($timeout)
51+
{
52+
$this->config(['connect_timeout' => $timeout]);
53+
}
54+
55+
/**
56+
* @param array $parameters
57+
*/
58+
public function config(array $parameters)
59+
{
60+
foreach ($parameters as $key => $value) {
61+
$this->config[$key] = $value;
62+
}
63+
}
64+
2165
/**
2266
* @return Client
2367
*/
@@ -44,18 +88,19 @@ public function setClient($client)
4488
public function readLink(LinkInterface $link)
4589
{
4690
$client = $this->getClient();
47-
$jar = new CookieJar();
4891

49-
$response = $client->request('GET', $link->getUrl(), [
50-
'allow_redirects' => ['max' => 10],
51-
'cookies' => $jar,
52-
'on_stats' => function (TransferStats $stats) use (&$link) {
53-
$link->setEffectiveUrl($stats->getEffectiveUri());
54-
}
55-
]);
56-
57-
$link->setContent($response->getBody())
58-
->setContentType($response->getHeader('Content-Type')[0]);
92+
try {
93+
$response = $client->request('GET', $link->getUrl(), array_merge($this->config, [
94+
'on_stats' => function (TransferStats $stats) use (&$link) {
95+
$link->setEffectiveUrl($stats->getEffectiveUri());
96+
}
97+
]));
98+
99+
$link->setContent($response->getBody())
100+
->setContentType($response->getHeader('Content-Type')[0]);
101+
} catch (ConnectException $e) {
102+
$link->setContent(false)->setContentType(false);
103+
}
59104

60105
return $link;
61106
}

0 commit comments

Comments
 (0)