* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\TaskBundle\Templating\UI; use Chill\MainBundle\Templating\UI\NotificationCounterInterface; use Chill\MainBundle\Entity\User; use Chill\TaskBundle\Repository\SingleTaskRepository; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Workflow\Event\Event; /** * * * @author Julien Fastré */ class CountNotificationTask implements NotificationCounterInterface { /** * * @var SingleTaskRepository */ protected $singleTaskRepository; /** * * @var CacheItempPoolInterface */ protected $cachePool; const CACHE_KEY = 'chill_task.count_notifications.user.%d'; public function __construct( SingleTaskRepository $singleTaskRepository, CacheItemPoolInterface $cachePool ) { $this->singleTaskRepository = $singleTaskRepository; $this->cachePool = $cachePool; } public function countNotification(User $u): int { $sumCache = $this->cachePool->getItem($this->getCacheKey($u)); if ($sumCache->isHit()) { return $sumCache->get(); } $params = [ 'user' => $u, 'is_closed' => false ]; $sum = 0; foreach ([ SingleTaskRepository::DATE_STATUS_ENDED, SingleTaskRepository::DATE_STATUS_WARNING] as $status) { $sum += $this->singleTaskRepository->countByParameters( \array_merge($params, [ 'date_status' => $status ]) ); } $sumCache->set($sum); $this->cachePool->save($sumCache); return $sum; } public function addNotification(User $u): int { return $this->countNotification($u); } public function resetCacheOnNewStates(Event $e) { /* @var $task \Chill\TaskBundle\Entity\SingleTask */ $task = $e->getSubject(); if (NULL !== $task->getAssignee()) { $sumCache = $this->cachePool->getItem($this->getCacheKey($task->getAssignee())); if ($sumCache->isHit()) { $this->cachePool->deleteItem($this->getCacheKey($task->getAssignee())); } } } private function getCacheKey(User $u) { return sprintf(self::CACHE_KEY, $u->getId()); } }