From eb06f147d9bd6f48c4aa2f683ef7d281c16e3ce6 Mon Sep 17 00:00:00 2001 From: Kilderson Sena Date: Sun, 19 Sep 2021 17:12:19 -0300 Subject: [PATCH] decreasing the short url path to 5 --- src/Controllers/ShortenUrlController.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Controllers/ShortenUrlController.php b/src/Controllers/ShortenUrlController.php index f65e189..b99b89b 100644 --- a/src/Controllers/ShortenUrlController.php +++ b/src/Controllers/ShortenUrlController.php @@ -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();