newslettersRepository = $newslettersRepository; $this->newsletterStatisticsRepository = $newsletterStatisticsRepository; $this->wooCommerceHelper = $wooCommerceHelper; } public function getStatisticsForAutomation(Automation $automation, Query $query): array { $emails = $this->getEmailsFromAutomation($automation); $formattedEmptyRevenue = $this->wooCommerceHelper->getRawPrice( 0, [ 'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(), ] ); $data = [ 'total' => ['current' => 0, 'previous' => 0], 'opened' => ['current' => 0, 'previous' => 0], 'clicked' => ['current' => 0, 'previous' => 0], 'orders' => ['current' => 0, 'previous' => 0], 'revenue' => ['current' => 0, 'previous' => 0], 'revenue_formatted' => [ 'current' => $formattedEmptyRevenue, 'previous' => $formattedEmptyRevenue, ], ]; if (!$emails) { return $data; } $requiredData = [ 'totals', StatisticsClickEntity::class, StatisticsOpenEntity::class, WooCommerceRevenue::class, ]; $currentStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( $emails, $query->getPrimaryAfter(), $query->getPrimaryBefore(), $requiredData ); foreach ($currentStatistics as $statistic) { $data['total']['current'] += $statistic->getTotalSentCount(); $data['opened']['current'] += $statistic->getOpenCount(); $data['clicked']['current'] += $statistic->getClickCount(); $data['orders']['current'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0; $data['revenue']['current'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0; } $previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( $emails, $query->getSecondaryAfter(), $query->getSecondaryBefore(), $requiredData ); foreach ($previousStatistics as $statistic) { $data['total']['previous'] += $statistic->getTotalSentCount(); $data['opened']['previous'] += $statistic->getOpenCount(); $data['clicked']['previous'] += $statistic->getClickCount(); $data['orders']['previous'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0; $data['revenue']['previous'] += $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0; } $data['revenue_formatted']['current'] = $this->wooCommerceHelper->getRawPrice( $data['revenue']['current'], [ 'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(), ] ); $data['revenue_formatted']['previous'] = $this->wooCommerceHelper->getRawPrice( $data['revenue']['previous'], [ 'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(), ] ); return $data; } private function getEmailsFromAutomation(Automation $automation): array { return array_filter(array_map( function(Step $step) { $emailId = $step->getArgs()['email_id'] ?? null; if (!$emailId) { return null; } return $this->newslettersRepository->findOneById((int)$emailId); }, array_filter( array_values($automation->getSteps()), function ($step) { return in_array( $step->getKey(), [ SendEmailAction::KEY, ], true ); } ) )); } }