-
Notifications
You must be signed in to change notification settings - Fork 31
Integration of Short.cm provider #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
felixmaier1989
wants to merge
11
commits into
mremi:master
Choose a base branch
from
felixmaier1989:short-cm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
56bf69d
Integration of Short.cm provider
felixmaier1989 c8409ba
CS
felixmaier1989 558b030
CS
felixmaier1989 3aadfc1
Coding standards, Code review
felixmaier1989 d10f0b9
Coding standards
felixmaier1989 b1c6080
Integration of Short.cm provider (expand method)
felixmaier1989 38352e3
Integration of Short.cm provider (change log)
felixmaier1989 0c8ef1d
Fix Travis (Composer dropped support for HHVM, see https://github.com…
felixmaier1989 51cc35a
Coding standards
felixmaier1989 c62f6a2
PHPdoc
felixmaier1989 9a60c2b
CS
felixmaier1989 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
src/Mremi/UrlShortener/Provider/ShortCm/ShortCmProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Mremi\UrlShortener library. | ||
| * | ||
| * (c) Rémi Marseille <marseille.remi@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mremi\UrlShortener\Provider\ShortCm; | ||
|
|
||
| use GuzzleHttp\Client; | ||
| use Mremi\UrlShortener\Exception\InvalidApiResponseException; | ||
| use Mremi\UrlShortener\Model\LinkInterface; | ||
| use Mremi\UrlShortener\Provider\UrlShortenerProviderInterface; | ||
|
|
||
| /** | ||
| * Short.cm provider class. | ||
| */ | ||
| class ShortCmProvider implements UrlShortenerProviderInterface | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private $apiKey; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $domain; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param string $apiKey An API key | ||
| * @param string $domain Domain name you added to short.cm | ||
| */ | ||
| public function __construct($apiKey, $domain) | ||
| { | ||
| $this->apiKey = $apiKey; | ||
| $this->domain = $domain; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getName() | ||
| { | ||
| return 'shortcm'; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function shorten(LinkInterface $link) | ||
| { | ||
| $client = $this->createClient(); | ||
|
|
||
| $data = array( | ||
| 'domain' => $this->domain, | ||
| 'originalURL' => $link->getLongUrl(), | ||
| ); | ||
|
|
||
| $response = $client->post('/links', array( | ||
| 'json' => $data, | ||
| )); | ||
|
|
||
| $this->validate($response); | ||
|
|
||
| $body = json_decode($response->getBody()->__toString(), true); | ||
|
|
||
| $url = !empty($body['secureShortURL']) | ||
| ? $body['secureShortURL'] | ||
| : !empty($body['shortURL']) | ||
| ? $body['shortURL'] | ||
| : null; | ||
|
|
||
| $link->setShortUrl($url); | ||
| } | ||
|
|
||
| /** | ||
| * @todo Implement method expand() | ||
| * {@inheritdoc} | ||
| */ | ||
| public function expand(LinkInterface $link) | ||
| { | ||
| throw new \Exception('Not implemented'); | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Creates a client. | ||
| * | ||
| * This method is mocked in unit tests in order to not make a real request, | ||
| * so visibility must be protected or public. | ||
| * | ||
| * @return Client | ||
| */ | ||
| protected function createClient() | ||
| { | ||
| return new Client(array( | ||
| 'base_uri' => 'https://api.short.cm', | ||
| 'headers' => array( | ||
| 'Authorization' => $this->apiKey, | ||
| ), | ||
| )); | ||
| } | ||
|
|
||
| /** | ||
| * Validates the API response. | ||
| * | ||
| * @param \GuzzleHttp\Psr7\Response $response API response | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| * | ||
| * @throws InvalidApiResponseException | ||
| */ | ||
| private function validate($response) | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| { | ||
| if ($response->getStatusCode() !== 200) { | ||
| throw new InvalidApiResponseException('Short.cm API returned unexpected status code '.$response->getStatusCode()); | ||
| } | ||
|
|
||
| $body = $response->getBody()->__toString(); | ||
|
|
||
| if (empty($body)) { | ||
| throw new InvalidApiResponseException('Short.cm API returned an empty body'); | ||
| } | ||
|
|
||
| $body = json_decode($body, true); | ||
|
|
||
| if (empty($body)) { | ||
| throw new InvalidApiResponseException('Short.cm API response body isnt valid JSON'); | ||
| } | ||
|
|
||
| if (empty($body['secureShortURL']) && empty($body['shortURL'])) { | ||
| throw new InvalidApiResponseException('Short.cm API could not generate a short URL'); | ||
| } | ||
| } | ||
| } | ||
116 changes: 116 additions & 0 deletions
116
tests/Mremi/UrlShortener/Tests/Provider/ShortCm/ShortCmProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Mremi\UrlShortener library. | ||
| * | ||
| * (c) Rémi Marseille <marseille.remi@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mremi\UrlShortener\Tests\Provider\ShortCm; | ||
|
|
||
| use Mremi\UrlShortener\Provider\ShortCm\ShortCmProvider; | ||
|
|
||
| /** | ||
| * Tests ShortCmProvider class. | ||
| */ | ||
| class ShortCmProviderTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
| public function testShorten() | ||
| { | ||
| $provider = $this->getMock('\Mremi\UrlShortener\Provider\ShortCm\ShortCmProvider', array('createClient'), array( | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| 'ABCD1234', | ||
| 'abc.de', | ||
| )); | ||
| $client = $this->getMock('\GuzzleHttp\Client', array('post')); | ||
|
|
||
| $response = new \GuzzleHttp\Psr7\Response( | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| 200, | ||
| array(), | ||
| '{"id":12345678,"originalURL":"http://perdu.com?k=12345678901234567890","DomainId":98765,"archived":false,"path":"pgsYuBjuGtzn","redirectType":null,"OwnerId":12345,"updatedAt":"2019-05-10T05:18:24.928Z","createdAt":"2019-05-10T05:18:24.928Z","secureShortURL":"https://abc.de/pgsYuBjuGtzn","shortURL":"https://abc.de/pgsYuBjuGtzn","duplicate":false}' | ||
| ); | ||
|
|
||
| $client | ||
| ->expects($this->once()) | ||
| ->method('post') | ||
| ->with( | ||
| '/links', | ||
| array( | ||
| 'json' => array( | ||
| 'domain' => 'abc.de', | ||
| 'originalURL' => 'http://perdu.com?k=12345678901234567890', | ||
| ), | ||
| ) | ||
| ) | ||
| ->will($this->returnValue($response)); | ||
|
|
||
| $provider | ||
| ->expects($this->once()) | ||
| ->method('createClient') | ||
| ->will($this->returnValue($client)); | ||
|
|
||
| $link = new \Mremi\UrlShortener\Model\Link(); | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| $link->setLongUrl('http://perdu.com?k=12345678901234567890'); | ||
| $provider->shorten($link); | ||
|
|
||
| $this->assertSame('https://abc.de/pgsYuBjuGtzn', $link->getShortUrl()); | ||
| } | ||
|
|
||
| public function testValidate() | ||
| { | ||
| $response = new \GuzzleHttp\Psr7\Response( | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| 200, | ||
| array(), | ||
| '{"id":12345678,"originalURL":"http://perdu.com?k=12345678901234567890","DomainId":98765,"archived":false,"path":"pgsYuBjuGtzn","redirectType":null,"OwnerId":12345,"updatedAt":"2019-05-10T05:18:24.928Z","createdAt":"2019-05-10T05:18:24.928Z","secureShortURL":"https://abc.de/pgsYuBjuGtzn","shortURL":"https://abc.de/pgsYuBjuGtzn","duplicate":false}' | ||
| ); | ||
|
|
||
| $provider = new ShortCmProvider( | ||
| 'ABCD1234', | ||
| 'abc.de' | ||
| ); | ||
|
|
||
| $method = new \ReflectionMethod($provider, 'validate'); | ||
| $method->setAccessible(true); | ||
| $method->invoke($provider, $response); | ||
| } | ||
|
|
||
| public function testValidateIncomplete() | ||
| { | ||
| $response = new \GuzzleHttp\Psr7\Response( | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
| 200, | ||
| array(), | ||
| '{"id":12345678,"originalURL":"http://perdu.com?k=12345678901234567890","DomainId":98765,"archived":false,"path":"pgsYuBjuGtzn","redirectType":null,"OwnerId":12345,"updatedAt":"2019-05-10T05:18:24.928Z","createdAt":"2019-05-10T05:18:24.928Z","secureShortURL":"","shortURL":"","duplicate":false}' | ||
| ); | ||
|
|
||
| $provider = new ShortCmProvider( | ||
| 'ABCD1234', | ||
| 'abc.de' | ||
| ); | ||
|
|
||
| $this->setExpectedException('\Mremi\UrlShortener\Exception\InvalidApiResponseException'); | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
|
|
||
| $method = new \ReflectionMethod($provider, 'validate'); | ||
| $method->setAccessible(true); | ||
| $method->invoke($provider, $response); | ||
| } | ||
|
|
||
| public function testValidateError() | ||
| { | ||
| $response = new \GuzzleHttp\Psr7\Response( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a use statement |
||
| 409 | ||
| ); | ||
|
|
||
| $provider = new ShortCmProvider( | ||
| 'ABCD1234', | ||
| 'abc.de' | ||
| ); | ||
|
|
||
| $this->setExpectedException('\Mremi\UrlShortener\Exception\InvalidApiResponseException'); | ||
|
felixmaier1989 marked this conversation as resolved.
Outdated
|
||
|
|
||
| $method = new \ReflectionMethod($provider, 'validate'); | ||
| $method->setAccessible(true); | ||
| $method->invoke($provider, $response); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove extra whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still relevant