This commit is contained in:
David Remer
2023-06-06 16:12:31 +03:00
committed by Aschepikov
parent 702b695de9
commit 71c3f1b283

View File

@@ -14,62 +14,61 @@ use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
use MailPoet\WooCommerce\Helper; use MailPoet\WooCommerce\Helper;
class OverviewStatisticsController { class OverviewStatisticsController {
/** @var NewslettersRepository */
private $newslettersRepository;
/** @var NewslettersRepository */ /** @var NewsletterStatisticsRepository */
private $newslettersRepository; private $newsletterStatisticsRepository;
/** @var NewsletterStatisticsRepository */ /** @var Helper */
private $newsletterStatisticsRepository; private $wooCommerceHelper;
/** @var Helper */
private $wooCommerceHelper;
public function __construct( public function __construct(
NewslettersRepository $newslettersRepository, NewslettersRepository $newslettersRepository,
NewsletterStatisticsRepository $newsletterStatisticsRepository, NewsletterStatisticsRepository $newsletterStatisticsRepository,
Helper $wooCommerceHelper Helper $wooCommerceHelper
) { ) {
$this->newslettersRepository = $newslettersRepository; $this->newslettersRepository = $newslettersRepository;
$this->newsletterStatisticsRepository = $newsletterStatisticsRepository; $this->newsletterStatisticsRepository = $newsletterStatisticsRepository;
$this->wooCommerceHelper = $wooCommerceHelper; $this->wooCommerceHelper = $wooCommerceHelper;
} }
public function getStatisticsForAutomation(Automation $automation, Query $query): array { public function getStatisticsForAutomation(Automation $automation, Query $query): array {
$emails = $this->getEmailsFromAutomation($automation); $emails = $this->getEmailsFromAutomation($automation);
$formattedEmptyRevenue = $this->wooCommerceHelper->getRawPrice( $formattedEmptyRevenue = $this->wooCommerceHelper->getRawPrice(
0, 0,
[ [
'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(), 'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(),
] ]
); );
$data = [ $data = [
'total' => ['current' => 0, 'previous' => 0], 'total' => ['current' => 0, 'previous' => 0],
'opened' => ['current' => 0, 'previous' => 0], 'opened' => ['current' => 0, 'previous' => 0],
'clicked' => ['current' => 0, 'previous' => 0], 'clicked' => ['current' => 0, 'previous' => 0],
'orders' => ['current' => 0, 'previous' => 0], 'orders' => ['current' => 0, 'previous' => 0],
'revenue' => ['current' => 0, 'previous' => 0], 'revenue' => ['current' => 0, 'previous' => 0],
'revenue_formatted' => [ 'revenue_formatted' => [
'current' => $formattedEmptyRevenue, 'current' => $formattedEmptyRevenue,
'previous' => $formattedEmptyRevenue, 'previous' => $formattedEmptyRevenue,
], ],
]; ];
if (!$emails) { if (!$emails) {
return $data; return $data;
} }
$requiredData = [ $requiredData = [
'totals', 'totals',
StatisticsClickEntity::class, StatisticsClickEntity::class,
StatisticsOpenEntity::class, StatisticsOpenEntity::class,
WooCommerceRevenue::class, WooCommerceRevenue::class,
]; ];
$currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( $currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
$emails, $emails,
$query->getPrimaryAfter(), $query->getPrimaryAfter(),
$query->getPrimaryBefore(), $query->getPrimaryBefore(),
$requiredData $requiredData
); );
foreach ($currentStatistics as $statistic) { foreach ($currentStatistics as $statistic) {
$data['total']['current'] += $statistic->getTotalSentCount(); $data['total']['current'] += $statistic->getTotalSentCount();
$data['opened']['current'] += $statistic->getOpenCount(); $data['opened']['current'] += $statistic->getOpenCount();
@@ -78,12 +77,12 @@ class OverviewStatisticsController {
$data['revenue']['current'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0; $data['revenue']['current'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0;
} }
$previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( $previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics(
$emails, $emails,
$query->getSecondaryAfter(), $query->getSecondaryAfter(),
$query->getSecondaryBefore(), $query->getSecondaryBefore(),
$requiredData $requiredData
); );
foreach ($previousStatistics as $statistic) { foreach ($previousStatistics as $statistic) {
$data['total']['previous'] += $statistic->getTotalSentCount(); $data['total']['previous'] += $statistic->getTotalSentCount();
@@ -108,30 +107,29 @@ class OverviewStatisticsController {
); );
return $data; return $data;
} }
private function getEmailsFromAutomation(Automation $automation): array { private function getEmailsFromAutomation(Automation $automation): array {
return array_filter(array_map( return array_filter(array_map(
function(Step $step) { function (Step $step) {
$emailId = $step->getArgs()['email_id'] ?? null; $emailId = $step->getArgs()['email_id'] ?? null;
if (!$emailId) { if (!$emailId) {
return null; return null;
} }
return $this->newslettersRepository->findOneById((int)$emailId); return $this->newslettersRepository->findOneById((int)$emailId);
}, },
array_filter( array_filter(
array_values($automation->getSteps()), array_values($automation->getSteps()),
function ($step) { function ($step) {
return in_array( return in_array(
$step->getKey(), $step->getKey(),
[ [
SendEmailAction::KEY, SendEmailAction::KEY,
], ],
true true
); );
} }
) )
)); ));
} }
} }