Export
1 week ago
NewsletterStatistics.php
3 years ago
NewsletterStatisticsRepository.php
2 weeks ago
WooCommerceRevenue.php
3 years ago
index.php
3 years ago
WooCommerceRevenue.php
78 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Statistics; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WooCommerce\Helper; |
| 9 | |
| 10 | class WooCommerceRevenue { |
| 11 | |
| 12 | /** @var string */ |
| 13 | private $currency; |
| 14 | |
| 15 | /** @var float */ |
| 16 | private $value; |
| 17 | |
| 18 | /** @var int */ |
| 19 | private $ordersCount; |
| 20 | |
| 21 | /** @var Helper */ |
| 22 | private $wooCommerceHelper; |
| 23 | |
| 24 | public function __construct( |
| 25 | $currency, |
| 26 | $value, |
| 27 | $ordersCount, |
| 28 | Helper $wooCommerceHelper |
| 29 | ) { |
| 30 | $this->currency = $currency; |
| 31 | $this->value = $value; |
| 32 | $this->ordersCount = $ordersCount; |
| 33 | $this->wooCommerceHelper = $wooCommerceHelper; |
| 34 | } |
| 35 | |
| 36 | /** @return string */ |
| 37 | public function getCurrency() { |
| 38 | return $this->currency; |
| 39 | } |
| 40 | |
| 41 | /** @return int */ |
| 42 | public function getOrdersCount() { |
| 43 | return $this->ordersCount; |
| 44 | } |
| 45 | |
| 46 | /** @return float */ |
| 47 | public function getValue() { |
| 48 | return $this->value; |
| 49 | } |
| 50 | |
| 51 | /** @return string */ |
| 52 | public function getFormattedValue() { |
| 53 | return $this->wooCommerceHelper->getRawPrice($this->value, ['currency' => $this->currency]); |
| 54 | } |
| 55 | |
| 56 | /** @return string */ |
| 57 | public function getFormattedAverageValue(): string { |
| 58 | $average = 0; |
| 59 | if ($this->ordersCount > 0) { |
| 60 | $average = $this->value / $this->ordersCount; |
| 61 | } |
| 62 | return $this->wooCommerceHelper->getRawPrice($average, ['currency' => $this->currency]); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return array |
| 67 | */ |
| 68 | public function asArray() { |
| 69 | return [ |
| 70 | 'currency' => $this->currency, |
| 71 | 'value' => (float)$this->value, |
| 72 | 'count' => (int)$this->ordersCount, |
| 73 | 'formatted' => $this->getFormattedValue(), |
| 74 | 'formatted_average' => $this->getFormattedAverageValue(), |
| 75 | ]; |
| 76 | } |
| 77 | } |
| 78 |