Add count of bounced emails into API response

[MAILPOET-3766]
This commit is contained in:
Jan Lysý
2021-09-14 12:07:16 +02:00
committed by Veljko V
parent 4f5a607ec0
commit ccedd3a112
3 changed files with 28 additions and 2 deletions

View File

@ -16,16 +16,27 @@ class NewsletterStatistics {
/** @var int */ /** @var int */
private $unsubscribeCount; private $unsubscribeCount;
/** @var int */
private $bounceCount;
/** @var int */ /** @var int */
private $totalSentCount; private $totalSentCount;
/** @var WooCommerceRevenue|null */ /** @var WooCommerceRevenue|null */
private $wooCommerceRevenue; private $wooCommerceRevenue;
public function __construct($clickCount, $openCount, $unsubscribeCount, $totalSentCount, $wooCommerceRevenue) { public function __construct(
$clickCount,
$openCount,
$unsubscribeCount,
$bounceCount,
$totalSentCount,
$wooCommerceRevenue
) {
$this->clickCount = $clickCount; $this->clickCount = $clickCount;
$this->openCount = $openCount; $this->openCount = $openCount;
$this->unsubscribeCount = $unsubscribeCount; $this->unsubscribeCount = $unsubscribeCount;
$this->bounceCount = $bounceCount;
$this->totalSentCount = $totalSentCount; $this->totalSentCount = $totalSentCount;
$this->wooCommerceRevenue = $wooCommerceRevenue; $this->wooCommerceRevenue = $wooCommerceRevenue;
} }
@ -42,6 +53,10 @@ class NewsletterStatistics {
return $this->unsubscribeCount; return $this->unsubscribeCount;
} }
public function getBounceCount(): int {
return $this->unsubscribeCount;
}
public function getTotalSentCount(): int { public function getTotalSentCount(): int {
return $this->totalSentCount; return $this->totalSentCount;
} }
@ -64,6 +79,7 @@ class NewsletterStatistics {
'opened' => $this->openCount, 'opened' => $this->openCount,
'machineOpened' => $this->machineOpenCount, 'machineOpened' => $this->machineOpenCount,
'unsubscribed' => $this->unsubscribeCount, 'unsubscribed' => $this->unsubscribeCount,
'bounced' => $this->bounceCount,
'revenue' => empty($this->wooCommerceRevenue) ? null : $this->wooCommerceRevenue->asArray(), 'revenue' => empty($this->wooCommerceRevenue) ? null : $this->wooCommerceRevenue->asArray(),
]; ];
} }

View File

@ -5,6 +5,7 @@ namespace MailPoet\Newsletter\Statistics;
use MailPoet\Doctrine\Repository; use MailPoet\Doctrine\Repository;
use MailPoet\Entities\NewsletterEntity; use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\ScheduledTaskEntity; use MailPoet\Entities\ScheduledTaskEntity;
use MailPoet\Entities\StatisticsBounceEntity;
use MailPoet\Entities\StatisticsClickEntity; use MailPoet\Entities\StatisticsClickEntity;
use MailPoet\Entities\StatisticsOpenEntity; use MailPoet\Entities\StatisticsOpenEntity;
use MailPoet\Entities\StatisticsUnsubscribeEntity; use MailPoet\Entities\StatisticsUnsubscribeEntity;
@ -37,6 +38,7 @@ class NewsletterStatisticsRepository extends Repository {
$this->getStatisticsClickCount($newsletter), $this->getStatisticsClickCount($newsletter),
$this->getStatisticsOpenCount($newsletter), $this->getStatisticsOpenCount($newsletter),
$this->getStatisticsUnsubscribeCount($newsletter), $this->getStatisticsUnsubscribeCount($newsletter),
$this->getStatisticsBounceCount($newsletter),
$this->getTotalSentCount($newsletter), $this->getTotalSentCount($newsletter),
$this->getWooCommerceRevenue($newsletter) $this->getWooCommerceRevenue($newsletter)
); );
@ -53,6 +55,7 @@ class NewsletterStatisticsRepository extends Repository {
$clickCounts = $this->getStatisticCounts(StatisticsClickEntity::class, $newsletters); $clickCounts = $this->getStatisticCounts(StatisticsClickEntity::class, $newsletters);
$openCounts = $this->getStatisticCounts(StatisticsOpenEntity::class, $newsletters); $openCounts = $this->getStatisticCounts(StatisticsOpenEntity::class, $newsletters);
$unsubscribeCounts = $this->getStatisticCounts(StatisticsUnsubscribeEntity::class, $newsletters); $unsubscribeCounts = $this->getStatisticCounts(StatisticsUnsubscribeEntity::class, $newsletters);
$bounceCounts = $this->getStatisticCounts(StatisticsBounceEntity::class, $newsletters);
$wooCommerceRevenues = $this->getWooCommerceRevenues($newsletters); $wooCommerceRevenues = $this->getWooCommerceRevenues($newsletters);
$statistics = []; $statistics = [];
@ -62,6 +65,7 @@ class NewsletterStatisticsRepository extends Repository {
$clickCounts[$id] ?? 0, $clickCounts[$id] ?? 0,
$openCounts[$id] ?? 0, $openCounts[$id] ?? 0,
$unsubscribeCounts[$id] ?? 0, $unsubscribeCounts[$id] ?? 0,
$bounceCounts[$id] ?? 0,
$totalSentCounts[$id] ?? 0, $totalSentCounts[$id] ?? 0,
$wooCommerceRevenues[$id] ?? null $wooCommerceRevenues[$id] ?? null
); );
@ -100,6 +104,11 @@ class NewsletterStatisticsRepository extends Repository {
return $counts[$newsletter->getId()] ?? 0; return $counts[$newsletter->getId()] ?? 0;
} }
public function getStatisticsBounceCount(NewsletterEntity $newsletter): int {
$counts = $this->getStatisticCounts(StatisticsBounceEntity::class, [$newsletter]);
return $counts[$newsletter->getId()] ?? 0;
}
public function getWooCommerceRevenue(NewsletterEntity $newsletter) { public function getWooCommerceRevenue(NewsletterEntity $newsletter) {
$revenues = $this->getWooCommerceRevenues([$newsletter]); $revenues = $this->getWooCommerceRevenues([$newsletter]);
return $revenues[$newsletter->getId()] ?? null; return $revenues[$newsletter->getId()] ?? null;

View File

@ -27,11 +27,12 @@ class NewslettersResponseBuilderTest extends \MailPoetTest {
'opened' => 6, 'opened' => 6,
'clicked' => 4, 'clicked' => 4,
'unsubscribed' => 2, 'unsubscribed' => 2,
'bounced' => 1,
'machineOpened' => 9, 'machineOpened' => 9,
'revenue' => null, 'revenue' => null,
], ],
]; ];
$statistics = new NewsletterStatistics(4, 6, 2, 10, null); $statistics = new NewsletterStatistics(4, 6, 2, 1, 10, null);
$statistics->setMachineOpenCount(9); $statistics->setMachineOpenCount(9);
$newsletterStatsRepository = Stub::make(NewsletterStatisticsRepository::class, [ $newsletterStatsRepository = Stub::make(NewsletterStatisticsRepository::class, [
'getTotalSentCount' => $stats['total_sent'], 'getTotalSentCount' => $stats['total_sent'],