Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

Commit 5e8d68b

Browse files
GenericResolver provider
1 parent fca694c commit 5e8d68b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace LinkShrink\Exception;
4+
5+
class MethodNotImplemented extends \RuntimeException implements Exception
6+
{
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace LinkShrink\Provider;
4+
5+
use Ivory\HttpAdapter\HttpAdapterException;
6+
use LinkShrink\Exception\MethodNotImplemented;
7+
8+
class GenericResolver extends AbstractProvider implements Provider
9+
{
10+
/**
11+
* @inheritdoc
12+
*/
13+
public function getName()
14+
{
15+
return 'generic_resolver';
16+
}
17+
18+
/**
19+
* @param string$url
20+
*
21+
* @return void
22+
* @throws MethodNotImplemented
23+
*/
24+
public function getShortenedUrl($url)
25+
{
26+
throw new MethodNotImplemented(sprintf('%s::%s not implimented.', get_class($this), 'shorten'));
27+
}
28+
29+
/**
30+
* @param string $url
31+
*
32+
* @return string
33+
*/
34+
public function getExpandedUrl($url)
35+
{
36+
/**
37+
* Attempt to use a HEAD request to fetch the redirect location, sometimes fails due to
38+
* incorrect Content-Length header in the response
39+
*/
40+
try {
41+
return $this->getAdapter()->head($url)->getHeader('location');
42+
} catch (HttpAdapterException $e) {
43+
return $this->getAdapter()->get($url)->getHeader('location');
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)