-
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 9 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
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 |
|---|---|---|
|
|
@@ -11,7 +11,8 @@ php: | |
| - 5.6 | ||
| - 7.0 | ||
| - 7.1 | ||
| - hhvm | ||
| - 7.2 | ||
| - 7.3 | ||
|
|
||
| before_install: | ||
| - composer self-update | ||
|
|
||
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
191 changes: 191 additions & 0 deletions
191
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,191 @@ | ||
| <?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 GuzzleHttp\Psr7\Response; | ||
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function expand(LinkInterface $link) | ||
| { | ||
| $client = $this->createClient(); | ||
|
|
||
| $parsed = parse_url($link->getShortUrl()); | ||
| $path = preg_replace('@^/(.*)$@', '$1', $parsed['path']); | ||
|
|
||
| $data = array( | ||
| 'domain' => $this->domain, | ||
| 'path' => $path, | ||
| ); | ||
|
|
||
| $response = $client->get('/links/expand', array( | ||
| 'query' => $data, | ||
| )); | ||
|
|
||
| $this->validateResponseExpand($response); | ||
|
|
||
| $body = json_decode($response->getBody()->__toString(), true); | ||
|
|
||
| $url = !empty($body['originalURL']) | ||
| ? $body['originalURL'] | ||
| : null; | ||
|
|
||
| $link->setLongUrl($url); | ||
| } | ||
|
|
||
| /** | ||
| * 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 Response $response API response | ||
| * | ||
| * @throws InvalidApiResponseException | ||
| */ | ||
| private function validate(Response $response) | ||
| { | ||
| 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'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates the API response for an Expand request. | ||
| * | ||
| * @param Response $response API response | ||
| * | ||
| * @throws InvalidApiResponseException | ||
| */ | ||
| private function validateResponseExpand(Response $response) | ||
| { | ||
| 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['originalURL'])) { | ||
| throw new InvalidApiResponseException('Short.cm API could not expand this URL'); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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