100 lines
3.0 KiB
PHP
100 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Stylist\Services;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Blog\Models\Blog;
|
|
use Modules\Faq\Models\Faq;
|
|
use Modules\News\Models\News;
|
|
use Modules\Products\Models\Product;
|
|
use Modules\Team\Models\TeamMember;
|
|
use Modules\Testimonials\Models\Testimonial;
|
|
use Throwable;
|
|
|
|
final class FrontendContent
|
|
{
|
|
/** @return array<string, mixed> */
|
|
public function homepage(array $settings): array
|
|
{
|
|
return [
|
|
'pricingPlans' => $this->enabled($settings, 'show_pricing', true) ? $this->pricingPlans() : collect(),
|
|
'testimonials' => $this->enabled($settings, 'show_testimonials', true) ? $this->testimonials() : collect(),
|
|
'faqs' => $this->enabled($settings, 'show_faq', true) ? $this->faqs() : collect(),
|
|
'teamMembers' => $this->enabled($settings, 'show_team', false) ? $this->team() : collect(),
|
|
'latestBlogPosts' => $this->enabled($settings, 'show_blog', true) ? $this->blogPosts() : collect(),
|
|
'latestNewsArticles' => $this->enabled($settings, 'show_news', true) ? $this->newsArticles() : collect(),
|
|
];
|
|
}
|
|
|
|
private function pricingPlans(): Collection
|
|
{
|
|
return $this->query('products', fn () => Product::query()
|
|
->where('is_active', true)
|
|
->where('product_type', 'plan')
|
|
->orderBy('sort_order')
|
|
->orderBy('id')
|
|
->limit(6)
|
|
->get());
|
|
}
|
|
|
|
private function testimonials(): Collection
|
|
{
|
|
return $this->query('testimonials', fn () => Testimonial::query()
|
|
->where('enabled', true)
|
|
->latest('id')
|
|
->limit(6)
|
|
->get());
|
|
}
|
|
|
|
private function faqs(): Collection
|
|
{
|
|
return $this->query('faqs', fn () => Faq::query()
|
|
->where('enabled', true)
|
|
->orderBy('sort_order')
|
|
->limit(12)
|
|
->get());
|
|
}
|
|
|
|
private function team(): Collection
|
|
{
|
|
return $this->query('team_members', fn () => TeamMember::query()
|
|
->where('enabled', true)
|
|
->orderBy('sort_order')
|
|
->limit(8)
|
|
->get());
|
|
}
|
|
|
|
private function blogPosts(): Collection
|
|
{
|
|
return $this->query('blog_posts', fn () => Blog::query()
|
|
->published()
|
|
->latest('published_at')
|
|
->limit(3)
|
|
->get());
|
|
}
|
|
|
|
private function newsArticles(): Collection
|
|
{
|
|
return $this->query('news_articles', fn () => News::query()
|
|
->published()
|
|
->latest('published_at')
|
|
->limit(3)
|
|
->get());
|
|
}
|
|
|
|
private function query(string $table, callable $callback): Collection
|
|
{
|
|
try {
|
|
return Schema::hasTable($table) ? $callback() : collect();
|
|
} catch (Throwable) {
|
|
return collect();
|
|
}
|
|
}
|
|
|
|
private function enabled(array $settings, string $key, bool $default): bool
|
|
{
|
|
return filter_var($settings[$key] ?? $default, FILTER_VALIDATE_BOOL);
|
|
}
|
|
}
|