/** * Test if the provided token is not empty and working with bitly * * @param mixed $value * @param Constraint $constraint * @throws \Exception */ public function validate($value, Constraint $constraint) { $bitlyProvider = new BitlyProvider(new GenericAccessTokenAuthenticator($value)); $urlShortener = new UrlShortenerService($bitlyProvider); $link = new Link(); $link->setLongUrl('http://www.campaignchain.com'); try { $urlShortener->shorten($link); } catch (ExternalApiException $e) { $this->context->buildViolation($constraint->message)->setParameter('%string%', $value)->addViolation(); } catch (\Exception $e) { // rethrow it throw $e; } }
/** * Use shortener service to shorten url * * @param $url * @param integer $uniqueId * @return mixed */ protected function getShortenedUrl($url, $uniqueId = null) { // skip if localhost if (in_array(parse_url($url, PHP_URL_HOST), array('localhost', '127.0.0.1'))) { return $url; } $link = new Link(); $link->setLongUrl($url); $this->urlShortener->shorten($link); return $link->getShortUrl(); }