Skip to content

Commit 2752e04

Browse files
committed
WIP
1 parent dca7d6d commit 2752e04

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

app/Console/Commands/SyncArticleImages.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(): void
3131

3232
Article::published()->chunk(100, function ($articles) {
3333
$articles->each(function ($article) {
34-
if ($article->hasHeroImage()) {
34+
if ($this->shouldBeSynced($article)) {
3535
$imageData = $this->fetchUnsplashImageDataFromId($article->hero_image);
3636

3737
if (!is_null($imageData)) {
@@ -45,6 +45,15 @@ public function handle(): void
4545
});
4646
}
4747

48+
protected function shouldBeSynced(Article $article): bool
49+
{
50+
if ($article->hero_image && ! $article->hasHeroImage()) {
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
4857
protected function fetchUnsplashImageDataFromId(string $imageId): ?array
4958
{
5059
$response = Http::retry(3, 100, null, false)->withToken($this->accessKey, 'Client-ID')

app/Models/Article.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ public function excerpt(int $limit = 100): string
103103

104104
public function hasHeroImage(): bool
105105
{
106-
return $this->hero_image !== null;
106+
return $this->hero_image_url !== null;
107+
}
108+
109+
public function hasHeroImageAuthor(): bool
110+
{
111+
return $this->hero_image_author_name !== null &&
112+
$this->hero_image_author_url !== null;
107113
}
108114

109115
public function heroImage($width = 400, $height = 300): string

resources/views/articles/show.blade.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ class="w-full bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray
8585
</div>
8686
</div>
8787
</div>
88-
<p class="text-xs text-black/50 text-center mt-2">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></p>
88+
89+
@if($article->hasHeroImageAuthor())
90+
<p class="text-xs text-black/50 text-center mt-2">
91+
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>
92+
</p>
93+
@endif
8994
</div>
9095
</div>
9196

0 commit comments

Comments
 (0)