Exemple #1
0
 public function abridge()
 {
     $url = $this->getParam('url');
     if (empty($url)) {
         return $this->output(false, 'No URL is provided');
     }
     if (!filter_var($url, FILTER_VALIDATE_URL)) {
         return $this->output(false, 'The given URL is malformed');
     }
     if (strlen($url) > self::MAX_URL_LEN) {
         return $this->output(false, 'The given URL exceeds the maximum length');
     }
     $query = $this->getService('DB')->prepare('INSERT INTO urls (url) VALUES (:url) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)');
     $query->bindParam(':url', $url);
     $query->execute();
     $url_id = $this->getService('DB')->lastInsertId();
     if (!$url_id) {
         return $this->output(false, 'Unable to generate a token for the given URL');
     }
     $token = Hasher::encode($url_id);
     return $this->output(true, 'OK', $token);
 }
 public function testEncode()
 {
     $this->assertEquals('zlwrymre', Hasher::encode(1));
     $this->assertEmpty(Hasher::encode(''));
 }