58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Krzysiej\RyobiCrawler\Twig;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Krzysiej\RyobiCrawler\Models\Price;
|
|
use Krzysiej\RyobiCrawler\Models\Product;
|
|
use Krzysiej\RyobiCrawler\Models\Stock;
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFilter;
|
|
use Twig\TwigFunction;
|
|
|
|
use function Symfony\Component\Clock\now;
|
|
|
|
class AppExtension extends AbstractExtension
|
|
{
|
|
public function getFunctions(): array
|
|
{
|
|
return [
|
|
new TwigFunction('promosCount', [$this, 'promosCount']),
|
|
new TwigFunction('allCount', [$this, 'allCount']),
|
|
new TwigFunction('newCount', [$this, 'newCount']),
|
|
new TwigFunction('discontinuedCount', [$this, 'discontinuedCount']),
|
|
];
|
|
}
|
|
|
|
public function getFilters(): array
|
|
{
|
|
return [
|
|
new TwigFilter('findByCreatedAtDate', [$this, 'findByCreatedAtDate']),
|
|
];
|
|
}
|
|
public function allCount(): int
|
|
{
|
|
return Product::count();
|
|
}
|
|
|
|
public function promosCount(): int
|
|
{
|
|
return Product::whereHas('currentPrice', fn(Builder $query) => $query->whereColumn('price', '<', 'productStandardPrice'))->count();
|
|
}
|
|
|
|
public function newCount(): int
|
|
{
|
|
return Product::where('created_at', '>', now()->modify('-30 days')->format('Y-m-d'))->count();
|
|
}
|
|
|
|
public function discontinuedCount(): int
|
|
{
|
|
return Product::where('updated_at', '<', now()->format('Y-m-d'))->count();
|
|
}
|
|
|
|
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null
|
|
{
|
|
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
|
}
|
|
} |