Skip to content

Commit

Permalink
Handle get web title from UHubLinkService
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Jan 10, 2023
1 parent ca616dc commit eb68041
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
41 changes: 0 additions & 41 deletions app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
namespace App\Models;

use App\Models\Traits\Hashidable;
use Embed\Embed;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\Url\Url as SpatieUrl;

/**
* @property int|null $user_id
* @property string $short_url
* @property string $destination
* @property string $title
* @property int $clicks
* @property int $uniqueClicks
*/
Expand Down Expand Up @@ -100,23 +96,6 @@ protected function destination(): Attribute
);
}

protected function title(): Attribute
{
return Attribute::make(
set: function ($value) {
if (config('urlhub.web_title')) {
if (Str::startsWith($value, 'http')) {
return $this->getWebTitle($value);
}

return $value;
}

return 'No Title';
},
);
}

protected function clicks(): Attribute
{
return Attribute::make(
Expand Down Expand Up @@ -204,24 +183,4 @@ public function totalClick(): int
{
return Visit::count();
}

/**
* Fetch the page title from the web page URL
*
* @throws \Exception
*/
public function getWebTitle(string $webAddress): string
{
$spatieUrl = SpatieUrl::fromString($webAddress);
$defaultTitle = $spatieUrl->getHost().' - Untitled';

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

return $webTitle;
}
}
28 changes: 27 additions & 1 deletion app/Services/UHubLinkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Http\Requests\StoreUrl;
use App\Models\Url;
use Embed\Embed;
use Illuminate\Http\Request;
use Spatie\Url\Url as SpatieUrl;

class UHubLinkService
{
Expand All @@ -27,7 +29,7 @@ public function create(StoreUrl $request): Url
return Url::create([
'user_id' => auth()->id(),
'destination' => $request->long_url,
'title' => $request->long_url,
'title' => $this->title($request->long_url),
'keyword' => $this->urlKey($request),
'is_custom' => $this->isCustom($request),
'ip' => $request->ip(),
Expand Down Expand Up @@ -67,6 +69,30 @@ public function duplicate(string $urlKey)
return $replicate->save();
}

/**
* Fetch the page title from the web page URL
*
* @throws \Exception
*/
public function title(string $webAddress): string
{
$spatieUrl = SpatieUrl::fromString($webAddress);
$defaultTitle = $spatieUrl->getHost().' - Untitled';

if (config('urlhub.web_title')) {
try {
$title = app(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';
}

private function urlKey(StoreUrl $request): string
{
return $request->custom_key ??
Expand Down
15 changes: 0 additions & 15 deletions tests/Unit/Models/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,6 @@ public function totalShortUrlByGuest()
$this->assertSame($expected, $actual);
}

/**
* @test
* @group u-model
*/
public function getWebTitle()
{
$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);
}

/**
* @test
* @group u-model
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Services/UHubLinkServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Unit\Services;

use App\Services\UHubLinkService;
use Tests\TestCase;

class UHubLinkServiceTest extends TestCase
{
private UHubLinkService $uHubLinkService;

protected function setUp(): void
{
parent::setUp();

$this->uHubLinkService = app(UHubLinkService::class);
}

/**
* @test
*/
public function title()
{
$expected = 'example123456789.com - Untitled';
$actual = $this->uHubLinkService->title('https://example123456789.com');
$this->assertSame($expected, $actual);

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

0 comments on commit eb68041

Please sign in to comment.