97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Krzysiej\RyobiCrawler\Twig;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Krzysiej\RyobiCrawler\Models\Price;
|
|
use Krzysiej\RyobiCrawler\Models\Product;
|
|
use Krzysiej\RyobiCrawler\Models\Stock;
|
|
use Symfony\Component\Routing\RouterInterface;
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFilter;
|
|
use Twig\TwigFunction;
|
|
|
|
use function Symfony\Component\Clock\now;
|
|
|
|
class AppExtension extends AbstractExtension
|
|
{
|
|
public function __construct(public RouterInterface $route)
|
|
{
|
|
}
|
|
|
|
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']),
|
|
new TwigFunction('lowestPriceCount', [$this, 'lowestPriceCount']),
|
|
new TwigFunction('renderCategoryTree', [$this, 'renderCategoryTree']),
|
|
];
|
|
}
|
|
|
|
public function getFilters(): array
|
|
{
|
|
return [
|
|
new TwigFilter('findByCreatedAtDate', [$this, 'findByCreatedAtDate']),
|
|
];
|
|
}
|
|
|
|
public function allCount(): int
|
|
{
|
|
return Product::count();
|
|
}
|
|
|
|
public function promosCount(): int
|
|
{
|
|
return Product::whereRaw('priceCurrent < 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('lastSeen', '<>', now()->format('Y-m-d'))->count();
|
|
}
|
|
|
|
public function lowestPriceCount(): int
|
|
{
|
|
return Product::whereRaw('priceCurrent = priceLowest')
|
|
->whereRaw('lastSeen = "' . now()->format('Y-m-d') . '"')
|
|
->whereRaw('priceCurrent < productStandardPrice')
|
|
->count();
|
|
}
|
|
|
|
public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null
|
|
{
|
|
return $items->first(fn($item) => str_starts_with($item->created_at, $date));
|
|
}
|
|
|
|
public function renderCategoryTree($categories, $level = 0): string
|
|
{
|
|
$tree = '';
|
|
if ($level == 0) {
|
|
$tree .= '<ul class="list-group">';
|
|
}
|
|
foreach ($categories as $categoryName => $category) {
|
|
$tree .= '<a class="list-group-item list-group-item-action text-decoration-none ms-' . ($level * 2) . '" href="' . $this->route->generate('app_category', ['category' => $categoryName]) . '">' . $categoryName . ' <span class="badge bg-primary rounded-pill">' . $category['count'] . '</span></a>';
|
|
|
|
unset($category['count']);
|
|
if (is_array($category) && count($category) >= 1) {
|
|
foreach ($category as $subcategoryName => $subCategory) {
|
|
$tree .= $this->renderCategoryTree([$subcategoryName => $subCategory], $level + 1);
|
|
}
|
|
}
|
|
}
|
|
if ($level == 0) {
|
|
$tree .= '</ul>';
|
|
}
|
|
return $tree;
|
|
|
|
}
|
|
}
|