CampaignGoalData.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\DataTransferObjects; |
| 4 | |
| 5 | use Give\Campaigns\CampaignDonationQuery; |
| 6 | use Give\Campaigns\CampaignSubscriptionQuery; |
| 7 | use Give\Campaigns\Models\Campaign; |
| 8 | use Give\Campaigns\ValueObjects\CampaignGoalType; |
| 9 | use Give\Framework\Support\Contracts\Arrayable; |
| 10 | |
| 11 | /** |
| 12 | * @since 4.0.0 |
| 13 | */ |
| 14 | class CampaignGoalData implements Arrayable |
| 15 | { |
| 16 | /** |
| 17 | * @var Campaign |
| 18 | */ |
| 19 | private $campaign; |
| 20 | |
| 21 | /** |
| 22 | * @var int |
| 23 | */ |
| 24 | public $actual; |
| 25 | |
| 26 | /** |
| 27 | * @var int |
| 28 | */ |
| 29 | public $percentage; |
| 30 | |
| 31 | /** |
| 32 | * @var int |
| 33 | */ |
| 34 | public $goal; |
| 35 | |
| 36 | /** |
| 37 | * @var int|string |
| 38 | */ |
| 39 | public $goalFormatted; |
| 40 | |
| 41 | /** |
| 42 | * @var int|string |
| 43 | */ |
| 44 | public $actualFormatted; |
| 45 | |
| 46 | /** |
| 47 | * @since 4.0.0 |
| 48 | */ |
| 49 | public function __construct(Campaign $campaign) |
| 50 | { |
| 51 | $this->campaign = $campaign; |
| 52 | $this->actual = $this->getActual(); |
| 53 | $this->actualFormatted = $this->getActualFormatted(); |
| 54 | $this->percentage = $this->getPercentage(); |
| 55 | $this->goal = $campaign->goal; |
| 56 | $this->goalFormatted = $this->getGoalFormatted(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @since 4.2.0 return union type int|float |
| 61 | * @since 4.0.0 |
| 62 | * |
| 63 | * @return int|float |
| 64 | */ |
| 65 | private function getActual() |
| 66 | { |
| 67 | $query = $this->campaign->goalType->isOneOf( |
| 68 | CampaignGoalType::SUBSCRIPTIONS(), |
| 69 | CampaignGoalType::AMOUNT_FROM_SUBSCRIPTIONS(), |
| 70 | CampaignGoalType::DONORS_FROM_SUBSCRIPTIONS() |
| 71 | ) |
| 72 | ? new CampaignSubscriptionQuery($this->campaign) |
| 73 | : new CampaignDonationQuery($this->campaign); |
| 74 | |
| 75 | switch ($this->campaign->goalType->getValue()) { |
| 76 | case CampaignGoalType::DONATIONS(): |
| 77 | case CampaignGoalType::SUBSCRIPTIONS(): |
| 78 | return $query->countDonations(); |
| 79 | |
| 80 | case CampaignGoalType::DONORS(): |
| 81 | case CampaignGoalType::DONORS_FROM_SUBSCRIPTIONS(): |
| 82 | return $query->countDonors(); |
| 83 | |
| 84 | case CampaignGoalType::AMOUNT_FROM_SUBSCRIPTIONS(): |
| 85 | return $query->sumInitialAmount(); |
| 86 | |
| 87 | case CampaignGoalType::AMOUNT(): |
| 88 | default: |
| 89 | return $query->sumIntendedAmount(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 4.0.0 |
| 95 | */ |
| 96 | private function getPercentage(): float |
| 97 | { |
| 98 | $percentage = $this->campaign->goal |
| 99 | ? $this->actual / $this->campaign->goal |
| 100 | : 0; |
| 101 | return round($percentage * 100, 2); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @since 4.0.0 |
| 106 | */ |
| 107 | private function getActualFormatted(): string |
| 108 | { |
| 109 | if ($this->campaign->goalType->isAmount()) { |
| 110 | return give_currency_filter(give_format_amount($this->actual)); |
| 111 | } |
| 112 | |
| 113 | return $this->actual; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @since 4.0.0 |
| 118 | */ |
| 119 | private function getGoalFormatted(): string |
| 120 | { |
| 121 | if ($this->campaign->goalType->isAmount()) { |
| 122 | return give_currency_filter(give_format_amount($this->goal)); |
| 123 | } |
| 124 | |
| 125 | return $this->goal; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @since 4.0.0 |
| 130 | */ |
| 131 | public function toArray(): array |
| 132 | { |
| 133 | return [ |
| 134 | 'actual' => $this->actual, |
| 135 | 'actualFormatted' => $this->actualFormatted, |
| 136 | 'percentage' => $this->percentage, |
| 137 | 'goal' => $this->goal, |
| 138 | 'goalFormatted' => $this->goalFormatted, |
| 139 | ]; |
| 140 | } |
| 141 | } |
| 142 |