Skip to content

Commit

Permalink
Refactor use of link lookups in new URL shortening fix #311
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrobolt committed Apr 11, 2017
1 parent 4286d20 commit d8dd8d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/Factories/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public static function createLink($long_url, $is_secret=false, $custom_ending=nu
looks like a shortened URL.');
}

if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url) !== false)) {
if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url, $creator) !== false)) {
// if link is not specified as secret, is non-custom, and
// already exists in Polr, lookup the value and return
$existing_link = LinkHelper::longLinkExists($long_url);
$existing_link = LinkHelper::longLinkExists($long_url, $creator);
return self::formatLink($existing_link);
}

Expand Down
22 changes: 18 additions & 4 deletions app/Helpers/LinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,30 @@ static public function linkExists($link_ending) {
}
}

static public function longLinkExists($long_url) {
static public function longLinkExists($long_url, $username=false) {
/**
* Provided a long link (string),
* check whether the link is in the DB.
* If a username is provided, only search for links created by the
* user.
* @return boolean
*/
$link = Link::longUrl($long_url)
$link_base = Link::longUrl($long_url)
->where('is_custom', 0)
->where('secret_key', '')
->first();
->where('secret_key', '');

if (is_null($username)) {
// Search for links without a creator only
$link = $link_base->where('creator', '')->first();
}
else if (($username !== false)) {
// Search for links created by $username only
$link = $link_base->where('creator', $username)->first();
}
else {
// Search for links created by any user
$link = $link_base->first();
}

if ($link == null) {
return false;
Expand Down

0 comments on commit d8dd8d6

Please sign in to comment.