-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Google Photo Suggestion Service
- Loading branch information
1 parent
a4b4b8b
commit 43afb80
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
app/Domains/Contact/ManagePhotos/Services/GooglePhoto.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace App\Domains\Contact\ManagePhotos\Services; | ||
|
||
use App\Interfaces\ServiceInterface; | ||
use App\Services\BaseService; | ||
use DOMDocument; | ||
use Exception; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class GooglePhoto extends BaseService implements ServiceInterface | ||
{ | ||
/** | ||
* Google search URL. | ||
*/ | ||
public const GOOGLE_SEARCH_URL = 'https://www.google.com/search'; | ||
|
||
/** | ||
* The params for Google search. | ||
*/ | ||
private array $params = [ | ||
'tbm' => 'isch', | ||
'tbs' => 'iar:xw,ift:png', | ||
]; | ||
|
||
/** | ||
* Get the permissions that apply to the user calling the service. | ||
*/ | ||
public function permissions(): array | ||
{ | ||
return [ | ||
'author_must_belong_to_account', | ||
'vault_must_belong_to_account', | ||
'author_must_be_vault_editor', | ||
'contact_must_belong_to_vault', | ||
]; | ||
} | ||
|
||
/** | ||
* Create a pet. | ||
* | ||
* @throws Exception | ||
*/ | ||
public function execute(string $searchTerm): array | ||
{ | ||
$html = $this->search($searchTerm); | ||
|
||
return $this->imageUrls($html); | ||
} | ||
|
||
/** | ||
* Set the params for the service. | ||
*/ | ||
public function params(array $params): self | ||
{ | ||
$this->params = $params; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Extract image URLs from the HTML. | ||
*/ | ||
private function imageUrls(string $html): array | ||
{ | ||
$imageUrls = []; | ||
|
||
if (empty($html)) { | ||
return $imageUrls; | ||
} | ||
|
||
$doc = new DOMDocument(); | ||
@$doc->loadHTML($html); | ||
|
||
$imgTags = $doc->getElementsByTagName('img'); | ||
|
||
foreach ($imgTags as $imgTag) { | ||
$src = $imgTag->getAttribute('src'); | ||
if (filter_var($src, FILTER_VALIDATE_URL)) { | ||
$imageUrls[] = $src; | ||
} | ||
} | ||
|
||
return $imageUrls; | ||
} | ||
|
||
/** | ||
* Fetch the HTML from Google. | ||
* | ||
* @throws Exception | ||
*/ | ||
private function search(string $searchTerm): string | ||
{ | ||
$params = array_merge($this->params, [ | ||
'q' => $searchTerm, | ||
]); | ||
|
||
try { | ||
$response = Http::get(self::GOOGLE_SEARCH_URL, $params); | ||
} catch (Exception $e) { | ||
throw new Exception('Failed to fetch data from Google.'); | ||
} | ||
|
||
return $response->body() ?? ''; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/Unit/Domains/Contact/ManageContact/Services/GooglePhotoTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Domains\Contact\ManageContact\Services; | ||
|
||
use App\Domains\Contact\ManagePhotos\Services\GooglePhoto; | ||
use Exception; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Support\Facades\Http; | ||
use Tests\TestCase; | ||
|
||
class GooglePhotoTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** | ||
* @test | ||
* | ||
* @group google-search | ||
* | ||
* @throws Exception | ||
*/ | ||
public function it_searches_google(): void | ||
{ | ||
Http::fake([ | ||
GooglePhoto::GOOGLE_SEARCH_URL.'/*' => Http::response('', 200), | ||
]); | ||
|
||
$service = new GooglePhoto(); | ||
|
||
$imageUrls = $service->execute('Laravel'); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @group google-fetch | ||
*/ | ||
public function it_fetches_image_urls(): void | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @group google-parse | ||
*/ | ||
public function it_parses_html(): void | ||
{ | ||
|
||
} | ||
} |