Skip to content

Commit

Permalink
decreasing the short url path to 5
Browse files Browse the repository at this point in the history
  • Loading branch information
dersonsena committed Sep 19, 2021
1 parent cfef863 commit eb06f14
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Controllers/ShortenUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,21 @@ public function __invoke(Request $request, Response $response, array $args): Res
VALUES (:id, :uuid, :long_url, :short_url_path, :created_at)
"));

$shortUrlPath = substr(sha1(uniqid((string)rand(), true)), 0, 10);
$shortUrlPath = substr(sha1(uniqid((string)rand(), true)), 0, 5);

while (true) {
$stmt2 = $this->db->prepare('select `id` from `urls` where `short_url_path` = :path');
$stmt2->execute(['path' => $shortUrlPath]);
$row = $stmt2->fetch();

if ($row !== false) {
$shortUrlPath = substr(sha1(uniqid((string)rand(), true)), 0, 5);
continue;
}

break;
}

$uuid = Uuid::uuid4();
$createdAt = new DateTimeImmutable();

Expand Down

0 comments on commit eb06f14

Please sign in to comment.