Skip to content

Commit

Permalink
add ability to store click headers
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Aug 22, 2024
1 parent 94472f8 commit f50f9fa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Repositories/ClickRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public static function findById(int $id, array $with = []): ShortUrlClick
/**
* @throws UrlRepositoryException
*/
public static function createClick(int $short_url_id, int $location_id, int $outcome_id): void
public static function createClick(int $short_url_id, int $location_id, int $outcome_id, array $headers): void
{
try {
ShortUrlClick::create([
'short_url_id' => $short_url_id,
'location_id' => $location_id,
'outcome_id' => $outcome_id,
'headers' => $headers,
'headers_signature' => md5(json_encode($headers))
]);
} catch (Exception $exception) {
throw new UrlRepositoryException($exception->getMessage());
Expand Down
5 changes: 3 additions & 2 deletions src/Services/ClickService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ClickService
/**
* @throws ClickServiceException
*/
public static function track(string $identifier, string $request_ip, int $outcome_id, ?string $domain = null): void
public static function track(string $identifier, string $request_ip, int $outcome_id, ?string $domain = null, array $headers = []): void
{
$request_ip = config('location.testing.enabled') ? config('location.testing.ip') : $request_ip;

Expand All @@ -63,7 +63,8 @@ public static function track(string $identifier, string $request_ip, int $outcom
? LocationRepository::getLocationFrom($request_ip)
: LocationRepository::locationUnknown($request_ip)
)->id,
$outcome_id
$outcome_id,
$headers
);
} catch (Exception $exception) {
throw new ClickServiceException($exception->getMessage());
Expand Down
2 changes: 2 additions & 0 deletions src/Utility/Factories/ShortUrlClickFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function definition()
'short_url_id' => ShortUrl::factory(),
'location_id' => ShortUrlLocation::factory(),
'outcome_id' => ClickService::$SUCCESS_ROUTED,
'headers' => $headers = ['Accept' => 'application/json'],
'headers_signature' => md5(json_encode($headers))
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('short_urls', function (Blueprint $table) {
$table->json('headers')->after('outcome_id')->nullable();
$table->string('headers_signature')->after('headers')->nullable();

$table->index('headers_signature');
});
}
};

0 comments on commit f50f9fa

Please sign in to comment.