@@ -67,8 +67,8 @@ public function __invoke(Request $request, Response $response, array $args): Res
67
67
}
68
68
69
69
$ stmt = $ this ->db ->prepare (trim ("
70
- INSERT INTO `urls` (`id`, `uuid`, `long_url`, `short_url_path`, `created_at`)
71
- VALUES (:id, :uuid, :long_url, :short_url_path, :created_at)
70
+ INSERT INTO `urls` (`id`, `uuid`, `long_url`, `short_url_path`, `economy_rate`, ` created_at`)
71
+ VALUES (:id, :uuid, :long_url, :short_url_path, :economy_rate, : created_at)
72
72
" ));
73
73
74
74
$ shortUrlPath = substr (sha1 (uniqid ((string )rand (), true )), 0 , 5 );
@@ -86,14 +86,17 @@ public function __invoke(Request $request, Response $response, array $args): Res
86
86
break ;
87
87
}
88
88
89
+ $ fullShortenUrl = $ this ->config ['baseUrl ' ] . '/ ' . $ shortUrlPath ;
89
90
$ uuid = Uuid::uuid4 ();
90
91
$ createdAt = new DateTimeImmutable ();
92
+ $ economyRate = ceil (100 - ((strlen ($ fullShortenUrl ) * 100 ) / strlen ($ contents ['huge_url ' ])));
91
93
92
94
$ stmt ->execute ([
93
95
'id ' => $ uuid ->getBytes (),
94
96
'uuid ' => $ uuid ->toString (),
95
97
'long_url ' => $ contents ['huge_url ' ],
96
98
'short_url_path ' => $ shortUrlPath ,
99
+ 'economy_rate ' => $ economyRate ,
97
100
'created_at ' => $ createdAt ->format ('Y-m-d H:i:s ' )
98
101
]);
99
102
@@ -105,8 +108,9 @@ public function __invoke(Request $request, Response $response, array $args): Res
105
108
'status ' => 'success ' ,
106
109
'data ' => [
107
110
'huge ' => $ contents ['huge_url ' ],
108
- 'shortened ' => $ this ->config ['baseUrl ' ] . '/ ' . $ shortUrlPath ,
109
- 'created_at ' => $ createdAt ->format (DateTimeInterface::ATOM )
111
+ 'shortened ' => $ fullShortenUrl ,
112
+ 'created_at ' => $ createdAt ->format (DateTimeInterface::ATOM ),
113
+ 'economyRate ' => $ economyRate
110
114
]
111
115
]));
112
116
0 commit comments