Geen omschrijving

WooCommerceRevenue.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace MailPoet\Newsletter\Statistics;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WooCommerce\Helper;
  5. class WooCommerceRevenue {
  6. /** @var string */
  7. private $currency;
  8. /** @var float */
  9. private $value;
  10. /** @var int */
  11. private $ordersCount;
  12. /** @var Helper */
  13. private $wooCommerceHelper;
  14. public function __construct(
  15. $currency,
  16. $value,
  17. $ordersCount,
  18. Helper $wooCommerceHelper
  19. ) {
  20. $this->currency = $currency;
  21. $this->value = $value;
  22. $this->ordersCount = $ordersCount;
  23. $this->wooCommerceHelper = $wooCommerceHelper;
  24. }
  25. /** @return string */
  26. public function getCurrency() {
  27. return $this->currency;
  28. }
  29. /** @return int */
  30. public function getOrdersCount() {
  31. return $this->ordersCount;
  32. }
  33. /** @return float */
  34. public function getValue() {
  35. return $this->value;
  36. }
  37. /** @return string */
  38. public function getFormattedValue() {
  39. return $this->wooCommerceHelper->getRawPrice($this->value, ['currency' => $this->currency]);
  40. }
  41. /** @return string */
  42. public function getFormattedAverageValue(): string {
  43. $average = 0;
  44. if ($this->ordersCount > 0) {
  45. $average = $this->value / $this->ordersCount;
  46. }
  47. return $this->wooCommerceHelper->getRawPrice($average, ['currency' => $this->currency]);
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function asArray() {
  53. return [
  54. 'currency' => $this->currency,
  55. 'value' => (float)$this->value,
  56. 'count' => (int)$this->ordersCount,
  57. 'formatted' => $this->getFormattedValue(),
  58. 'formatted_average' => $this->getFormattedAverageValue(),
  59. ];
  60. }
  61. }