Skip to content

Commit 93d2e31

Browse files
committed
wip
1 parent cc33a39 commit 93d2e31

File tree

6 files changed

+21
-72
lines changed

6 files changed

+21
-72
lines changed

app/Console/Commands/SyncArticleImages.php

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,34 @@ final class SyncArticleImages extends Command
1010
{
1111
protected $signature = 'lio:sync-article-images';
1212

13-
protected $description = 'Updates the Unsplash image for all articles';
14-
15-
protected $accessKey;
16-
17-
public function __construct()
18-
{
19-
parent::__construct();
20-
21-
$this->accessKey = config('services.unsplash.access_key');
22-
}
13+
protected $description = 'Updates the Unsplash image for all unsynced articles';
2314

2415
public function handle(): void
2516
{
26-
if (! $this->accessKey) {
17+
if (! config('services.unsplash.access_key')) {
2718
$this->error('Unsplash access key must be configured');
2819

2920
return;
3021
}
3122

32-
Article::published()->chunk(100, function ($articles) {
23+
Article::unsyncedImages()->chunk(100, function ($articles) {
3324
$articles->each(function ($article) {
34-
if ($this->checkShouldArticleBeSynced($article)) {
35-
$imageData = $this->fetchUnsplashImageDataFromId($article->hero_image_id);
25+
$imageData = $this->fetchUnsplashImageDataFromId($article->hero_image_id);
3626

37-
if (!is_null($imageData)) {
38-
$article->hero_image_url = $imageData['image_url'];
39-
$article->hero_image_author_name = $imageData['author_name'];
40-
$article->hero_image_author_url = $imageData['author_url'];
41-
$article->save();
42-
}
27+
if (! is_null($imageData)) {
28+
$article->hero_image_url = $imageData['image_url'];
29+
$article->hero_image_author_name = $imageData['author_name'];
30+
$article->hero_image_author_url = $imageData['author_url'];
31+
$article->save();
4332
}
4433
});
4534
});
4635
}
4736

48-
protected function checkShouldArticleBeSynced(Article $article): bool
49-
{
50-
if (!$article->hero_image_id || $article->hasHeroImage()) {
51-
return false;
52-
}
53-
54-
return true;
55-
}
56-
5737
protected function fetchUnsplashImageDataFromId(string $imageId): ?array
5838
{
59-
$response = Http::retry(3, 100, null, false)->withToken($this->accessKey, 'Client-ID')
39+
$response = Http::retry(3, 100, throw: false)
40+
->withToken(config('services.unsplash.access_key'), 'Client-ID')
6041
->get("https://api.unsplash.com/photos/{$imageId}");
6142

6243
if ($response->failed()) {

app/Models/Article.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ public function scopeTrending(Builder $query): Builder
318318
->orderBy('submitted_at', 'desc');
319319
}
320320

321+
public function scopeUnsyncedImages(Builder $query): Builder
322+
{
323+
return $query->whereNotNull('hero_image_id')
324+
->whereNull('hero_image_url');
325+
}
326+
321327
public function shouldBeSearchable()
322328
{
323329
return $this->isPublished();

database/migrations/2024_09_27_095949_add_hero_image_additional_columns_to_articles.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@ public function up(): void
1818
$table->string('hero_image_author_url')->nullable();
1919
});
2020
});
21-
}
2221

23-
/**
24-
* Reverse the migrations.
25-
*/
26-
public function down(): void
27-
{
2822
Schema::table('articles', function (Blueprint $table) {
29-
$table->dropColumn([
30-
'hero_image_url',
31-
'hero_image_author_name',
32-
'hero_image_author_url',
33-
]);
23+
$table->renameColumn('hero_image', 'hero_image_id');
3424
});
3525
}
3626
};

database/migrations/2024_10_03_065334_rename_hero_image_to_hero_image_id_in_articles.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

database/seeders/UserSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function run(): void
4444
[
4545
'submitted_at' => now(),
4646
'approved_at' => now(),
47-
'hero_image' => 'sxiSod0tyYQ',
47+
'hero_image_id' => 'sxiSod0tyYQ',
4848
],
4949
['submitted_at' => now(), 'approved_at' => now()],
5050
['submitted_at' => now()],

resources/views/articles/show.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class="w-full bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray
8686
</div>
8787
</div>
8888

89-
@if($article->hasHeroImageAuthor())
89+
@if ($article->hasHeroImageAuthor())
9090
<p class="text-xs text-black/50 text-center mt-2">
9191
Photo by <a class="underline font-medium" href="{{ $article->hero_image_author_url }}?ref=laravel.io" target="_blank">{{ $article->hero_image_author_name }} </a> on <a class="underline font-medium" href="https://unsplash.com/?ref=laravel.io" target="_blank">Unsplash</a>
9292
</p>
@@ -256,4 +256,4 @@ class="prose prose-lg text-gray-800 prose-lio"
256256
@endif
257257
</x-modal>
258258
@endcan
259-
@endsection
259+
@endsection

0 commit comments

Comments
 (0)