Skip to content

Commit

Permalink
Merge 8870dfd into e08e2be
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Oct 18, 2023
2 parents e08e2be + 8870dfd commit d0cd027
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 127 deletions.
6 changes: 4 additions & 2 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\Url;
use App\Models\User;
use App\Services\KeyGeneratorService;
use App\Services\UrlService;

class DashboardController extends Controller
{
Expand Down Expand Up @@ -51,7 +50,10 @@ public function edit(string $urlKey)
*/
public function update(StoreUrl $request, Url $hash_id)
{
app(UrlService::class)->update($request, $hash_id);
$hash_id->update([
'destination' => $request->long_url,
'title' => $request->title,
]);

return to_route('dashboard')
->withFlashSuccess(__('Link changed successfully !'));
Expand Down
11 changes: 9 additions & 2 deletions app/Http/Controllers/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use App\Http\Requests\StoreUrl;
use App\Models\Url;
use App\Models\User;
use App\Models\Visit;
use App\Services\QrCodeService;
use App\Services\UrlService;

class UrlController extends Controller
{
Expand All @@ -26,7 +26,14 @@ public function __construct()
*/
public function create(StoreUrl $request)
{
$url = app(UrlService::class)->create($request);
$url = Url::create([
'user_id' => auth()->id(),
'destination' => $request->long_url,
'title' => app(Url::class)->getWebTitle($request->long_url),
'keyword' => app(Url::class)->getKeyword($request),
'is_custom' => $request->custom_key ? true : false,
'user_sign' => app(User::class)->signature(),
]);

return to_route('su_detail', $url->keyword);
}
Expand Down
28 changes: 28 additions & 0 deletions app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Models;

use App\Http\Requests\StoreUrl;
use App\Services\KeyGeneratorService;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -123,6 +125,32 @@ protected function uniqueClicks(): Attribute
|--------------------------------------------------------------------------
*/

public function getKeyword(StoreUrl $request): string
{
$keyGen = app(KeyGeneratorService::class);

return $request->custom_key ?? $keyGen->generate($request->long_url);
}

public function getWebTitle(string $webAddress): string
{
$spatieUrl = \Spatie\Url\Url::fromString($webAddress);
$defaultTitle = $spatieUrl->getHost().' - Untitled';

if (config('urlhub.web_title')) {
try {
$title = app(\Embed\Embed::class)->get($webAddress)->title ?? $defaultTitle;
} catch (\Exception) {
// If failed or not found, then return "{domain_name} - Untitled"
$title = $defaultTitle;
}

return $title;
}

return 'No Title';
}

/**
* The number of shortened URLs that have been created by each User
*
Expand Down
78 changes: 0 additions & 78 deletions app/Services/UrlService.php

This file was deleted.

30 changes: 30 additions & 0 deletions tests/Unit/Models/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ public function getUniqueClicksAttribute(): void
|--------------------------------------------------------------------------
*/

/**
* @test
* @group u-model
*/
public function getWebTitle(): void
{
$expected = 'example123456789.com - Untitled';
$actual = $this->url->getWebTitle('https://example123456789.com');
$this->assertSame($expected, $actual);

$expected = 'www.example123456789.com - Untitled';
$actual = $this->url->getWebTitle('https://www.example123456789.com');
$this->assertSame($expected, $actual);
}

/**
* When config('urlhub.web_title') set `false`, title() should return
* 'No Title' if the title is empty
*
* @test
*/
public function getWebTitle_ShouldReturnNoTitle(): void
{
config(['urlhub.web_title' => false]);

$expected = 'No Title';
$actual = $this->url->getWebTitle('https://example123456789.com');
$this->assertSame($expected, $actual);
}

/**
* The number of shortened URLs that have been created by each User
*
Expand Down
45 changes: 0 additions & 45 deletions tests/Unit/Services/UrlServiceTest.php

This file was deleted.

0 comments on commit d0cd027

Please sign in to comment.