Skip to content

Commit

Permalink
update to always handle domain and move things to base option and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
yordadev committed Jun 27, 2024
1 parent fdd9523 commit 656e155
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Actions/ShortUrlRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ShortUrlRedirect extends Controller
*/
public function __invoke(Request $request, string $identifier)
{
dd($request, $identifier);
$this->identifier = $identifier;
/**
* Get Short URL Identifier & Validate
Expand Down
2 changes: 2 additions & 0 deletions src/Builders/UrlBuilder/Options/BaseOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class BaseOption implements UrlBuilderOptionInterface
*/
public function resolve(Collection &$shortUrlCollection): void
{
$domain = $shortUrlCollection->get('domain');
$identifier = $shortUrlCollection->has('identifier')
? $shortUrlCollection->get('identifier')
: $this->generateUrlIdentifier();

$shortUrlCollection = $shortUrlCollection->merge([
'identifier' => $identifier,
'domain' => $domain,
]);

$shortUrlArr = $shortUrlCollection->only([
Expand Down
1 change: 1 addition & 0 deletions src/Builders/UrlBuilder/Options/WithActivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function resolve(Collection &$shortUrlCollection): void
{
UrlRepository::updateShortUrl(
$shortUrlCollection->get('identifier'),
$shortUrlCollection->get('domain'),
['activation' => $shortUrlCollection->get('activation')]
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Builders/UrlBuilder/Options/WithDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use YorCreative\UrlShortener\Exceptions\UrlRepositoryException;
use YorCreative\UrlShortener\Repositories\UrlRepository;

/**
* @deprecated
*/
class WithDomain implements UrlBuilderOptionInterface
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/Builders/UrlBuilder/Options/WithOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function resolve(Collection &$shortUrlCollection): void
$primary_key = $model->getKeyName();

UrlService::attachOwnership(
$shortUrlCollection->get('domain'),
$shortUrlCollection->get('identifier'),
$model->getMorphClass(),
$model->$primary_key
Expand Down
1 change: 1 addition & 0 deletions src/Builders/UrlBuilder/Options/WithPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function resolve(Collection &$shortUrlCollection): void
{
UrlRepository::updateShortUrl(
$shortUrlCollection->get('identifier'),
$shortUrlCollection->get('domain'),
['password' => $shortUrlCollection->get('password')]
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Builders/UrlBuilder/Options/WithTracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public function resolve(Collection &$shortUrlCollection): void
);

$trace = [
'short_url_id' => UrlRepository::findByIdentifier(
$shortUrlCollection->get('identifier')
'short_url_id' => UrlRepository::findByDomainIdentifier(
$shortUrlCollection->get('domain'),
$shortUrlCollection->get('identifier'),
)->id,
];

Expand Down
8 changes: 5 additions & 3 deletions src/Builders/UrlBuilder/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ public function withDomain(string $domain): UrlBuilder
{
$this->shortUrlCollection->put('domain', $domain);

$this->options->add(
new WithDomain()
);
// $this->options->add(
// new WithDomain()
// );

// handled in baseOption now

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Repositories/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public static function create(array $ShortUrl): ShortUrl
/**
* @throws UrlRepositoryException
*/
public static function updateShortUrl(string $identifier, array $updates): ShortUrl
public static function updateShortUrl(string $identifier, string $domain, array $updates): ShortUrl
{
try {
$ShortUrlRecord = self::findByIdentifier($identifier);
$ShortUrlRecord = self::findByDomainIdentifier($domain, $identifier);
$ShortUrlRecord->update($updates);

return $ShortUrlRecord;
Expand Down
4 changes: 2 additions & 2 deletions src/Services/UrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public static function attempt(string $identifier, string $password): ?ShortUrl
/**
* @throws UrlRepositoryException
*/
public static function attachOwnership($identifier, $type, $id): void
public static function attachOwnership($domain, $identifier, $type, $id): void
{
try {
UrlRepository::findOrCreateOwnershipRecord([
'short_url_id' => UrlRepository::findByIdentifier($identifier)->id,
'short_url_id' => UrlRepository::findByDomainIdentifier($domain, $identifier)->id,
'ownerable_type' => $type,
'ownerable_id' => $id,
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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) {
// Drop the unique constraint on identifier
$table->dropUnique(['identifier']);

// Add the new unique composite key that should not
// allow more than one identifier per domain at a time
// soft deleting recycles the identifier
$table->unique(['domain', 'identifier', 'deleted_at']);
});
}
};

0 comments on commit 656e155

Please sign in to comment.