Skip to content

Commit

Permalink
Fix unnecessary access to sent mail count in MailLogs widget
Browse files Browse the repository at this point in the history
remp/crm#2725
  • Loading branch information
rootpd committed Jan 23, 2023
1 parent 918de5f commit 6ba386c
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions src/Components/MailLogs/MailLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,34 @@

class MailLogs extends Control implements WidgetInterface
{
private $view = 'mail_logs';
private string $view = 'mail_logs';

private $usersRepository;
private VisualPaginator $paginator;

private $translator;
private int $totalCount;

private $logQuery;

private $mailLogRepository;

private $totalCount;

/** @var VisualPaginator */
private $paginator;

/** @var UnclaimedUser */
private $unclaimedUser;
private bool $enabled = true;

public function __construct(
MailLogQueryBuilder $logQueryBuilder,
MailLogsRepository $mailLogRepository,
UsersRepository $usersRepository,
UnclaimedUser $unclaimedUser,
Translator $translator
private MailLogQueryBuilder $logQuery,
private MailLogsRepository $mailLogRepository,
private UsersRepository $usersRepository,
private UnclaimedUser $unclaimedUser,
private Translator $translator,
) {
$this->usersRepository = $usersRepository;
$this->translator = $translator;
$this->logQuery = $logQueryBuilder;
$this->mailLogRepository = $mailLogRepository;
$this->unclaimedUser = $unclaimedUser;
}

public function header($id = '')
{
$header = $this->translator->translate('remp_mailer.admin.mail_logs_component.header');
if ($id) {
$header .= Html::el('small')->setHtml(' (' . $this->totalCount($id) . ')');

$user = $this->usersRepository->find($id);
if ($user->deleted_at !== null || $this->unclaimedUser->isUnclaimedUser($user)) {
$this->enabled = false;
return $header;
}

$header .= Html::el('small')->setHtml(' (' . $this->totalCount($id) . ')');
return $header;
}

Expand All @@ -72,14 +62,12 @@ public function render($userId)
}

$user = $this->usersRepository->find($userId);
$notLoaded = false;
if ($user->deleted_at !== null || $this->unclaimedUser->isUnclaimedUser($user)) {
if (!$this->enabled) {
$total = 0;
$notLoaded = true;
} else {
$total = $this->totalCount($userId);

if ($this->paginator) {
if (isset($this->paginator)) {
$paginator = $this->paginator->getPaginator();
$paginator->setItemCount($total);
$paginator->setItemsPerPage(10);
Expand All @@ -100,7 +88,7 @@ public function render($userId)
}

$this->template->emails = $logs ?? [];
$this->template->notLoaded = $notLoaded;
$this->template->notLoaded = !$this->enabled;
$this->template->totals = [
'total' => $total,
'delivered' => $counts['delivered_at'] ?? 0,
Expand All @@ -117,7 +105,7 @@ public function render($userId)

private function totalCount($userId)
{
if ($this->totalCount === null) {
if (!isset($this->totalCount)) {
$user = $this->usersRepository->find($userId);
$counts = $this->mailLogRepository->count($user->email, ['sent_at']);
$this->totalCount = $counts['sent_at'] ?? 0;
Expand Down

0 comments on commit 6ba386c

Please sign in to comment.