<?php
declare(strict_types=1);
namespace App\Controller;
use App\Application;
use App\Entity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Cache;
class DashboardController extends AbstractController
{
public function __construct(
private Application\DashboardService $dashboardService,
) {}
public function home(Cache\CacheInterface $cache): Response
{
/** @var Entity\Agent */
$user = $this->getUser();
$cachePrefix = "{$user->getId()}::dashboard";
$waitingToPay = $cache->get("{$cachePrefix}::waiting_to_pay", function (Cache\ItemInterface $item) use ($user) {
$item->expiresAfter(5 * 60);
return $this->dashboardService->waitingToPaySummary($user);
});
$productionTotals = $cache->get("{$cachePrefix}::monthlyProduction", function (Cache\ItemInterface $item) use ($user) {
$item->expiresAfter(5 * 60);
return $this->dashboardService->monthlyProductionSummary($user);
});
$callbacks = $this->dashboardService->callbacksForAgents($user);
return $this->render(
'dashboard/home.html.twig',
[
'bulletins' => $this->dashboardService->bulletinSummary(4),
'production' => [
'monthly' => $productionTotals,
'total' => array_sum(array_column($productionTotals, 'value')),
],
'waitingToPay' => [
['label' => 'Advanced', 'value' => $waitingToPay['advanced']],
['label' => 'As Earned', 'value' => $waitingToPay['asEarned']],
],
'callbacks' => $callbacks,
]
);
}
}