CampaignPageRepository.php
7 months ago
CampaignRepository.php
1 year ago
CampaignsDataRepository.php
9 months ago
CampaignsDataRepository.php
242 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Repositories; |
| 4 | |
| 5 | use Give\Campaigns\CampaignsDataQuery; |
| 6 | use Give\Campaigns\Models\Campaign; |
| 7 | use Give\Campaigns\ValueObjects\CampaignGoalType; |
| 8 | |
| 9 | /** |
| 10 | * Used to optimize the campaigns list table performance and to avoid n+1 problems. |
| 11 | * Instead of doing expensive queries in multiple columns in each row, this class loads everything upfront for multiple campaigns. |
| 12 | * |
| 13 | * @since 4.0.0 |
| 14 | */ |
| 15 | class CampaignsDataRepository |
| 16 | { |
| 17 | /** |
| 18 | * @var array |
| 19 | */ |
| 20 | private $amounts; |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | private $subscriptionAmounts = []; |
| 25 | /** |
| 26 | * @var array |
| 27 | */ |
| 28 | private $donationsCount; |
| 29 | /** |
| 30 | * @var array |
| 31 | */ |
| 32 | private $subscriptionDonationsCount = []; |
| 33 | /** |
| 34 | * @var array |
| 35 | */ |
| 36 | private $donorsCount; |
| 37 | /** |
| 38 | * @var array |
| 39 | */ |
| 40 | private $subscriptionDonorsCount = []; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @since 4.8.0 added data caching layer |
| 45 | * |
| 46 | * @param int[] $ids |
| 47 | * |
| 48 | * @return CampaignsDataRepository |
| 49 | */ |
| 50 | public static function campaigns(array $ids): CampaignsDataRepository |
| 51 | { |
| 52 | $self = new self(); |
| 53 | $campaignsData = get_option('give_campaigns_data', []); |
| 54 | $campaignsSubscriptionData = get_option('give_campaigns_subscription_data', []); |
| 55 | |
| 56 | // remove cached campaign ids |
| 57 | $campaignIds = array_filter($ids, function ($id) use ($campaignsData) { |
| 58 | foreach ($campaignsData as $row) { |
| 59 | foreach($row as $campaign) { |
| 60 | if ($campaign['campaign_id'] == $id) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | }); |
| 68 | |
| 69 | if ( |
| 70 | ! empty($campaignsData['donationsCount']) |
| 71 | || ! empty($campaignsSubscriptionData['donationsCount']) |
| 72 | ) { |
| 73 | $self->amounts = $campaignsData['amounts']; |
| 74 | $self->donationsCount = $campaignsData['donationsCount']; |
| 75 | $self->donorsCount = $campaignsData['donorsCount']; |
| 76 | |
| 77 | if (defined('GIVE_RECURRING_VERSION')) { |
| 78 | $self->subscriptionAmounts = $campaignsSubscriptionData['amounts']; |
| 79 | $self->subscriptionDonationsCount = $campaignsSubscriptionData['donationsCount']; |
| 80 | $self->subscriptionDonorsCount = $campaignsSubscriptionData['donorsCount']; |
| 81 | } |
| 82 | |
| 83 | return $self; |
| 84 | } |
| 85 | |
| 86 | // Fetch data from db |
| 87 | $donations = CampaignsDataQuery::donations($campaignIds); |
| 88 | |
| 89 | $self->amounts = $donations->collectIntendedAmounts(); |
| 90 | $self->donationsCount = $donations->collectDonations(); |
| 91 | $self->donorsCount = $donations->collectDonors(); |
| 92 | |
| 93 | // cache campaigns data |
| 94 | update_option('give_campaigns_data', [ |
| 95 | 'amounts' => array_merge($campaignsData['amounts'] ?? [], $self->amounts), |
| 96 | 'donationsCount' => array_merge($campaignsData['donationsCount'] ?? [], $self->donationsCount), |
| 97 | 'donorsCount' => array_merge($campaignsData['donorsCount'] ?? [], $self->donorsCount), |
| 98 | ]); |
| 99 | |
| 100 | // Set subscriptions data |
| 101 | if (defined('GIVE_RECURRING_VERSION')) { |
| 102 | $subscriptions = CampaignsDataQuery::subscriptions($campaignIds); |
| 103 | |
| 104 | $self->subscriptionAmounts = $subscriptions->collectInitialAmounts(); |
| 105 | $self->subscriptionDonationsCount = $subscriptions->collectDonations(); |
| 106 | $self->subscriptionDonorsCount = $subscriptions->collectDonors(); |
| 107 | |
| 108 | // cache campaigns subscriptions data |
| 109 | update_option('give_campaigns_subscriptions_data', [ |
| 110 | 'amounts' => array_merge($campaignsSubscriptionData['amounts'] ?? [], $self->subscriptionAmounts), |
| 111 | 'donationsCount' => array_merge($campaignsSubscriptionData['donationsCount'] ?? [], $self->subscriptionDonationsCount), |
| 112 | 'donorsCount' => array_merge($campaignsSubscriptionData['donorsCount'] ?? [], $self->subscriptionDonorsCount), |
| 113 | ]); |
| 114 | } |
| 115 | |
| 116 | return $self; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @since 4.2.0 return type of float |
| 121 | * @since 4.0.0 |
| 122 | * |
| 123 | * Get revenue for campaign |
| 124 | * |
| 125 | * @param Campaign $campaign |
| 126 | */ |
| 127 | public function getRevenue(Campaign $campaign): float |
| 128 | { |
| 129 | $data = $campaign->goalType->isSubscriptions() |
| 130 | ? $this->subscriptionAmounts |
| 131 | : $this->amounts; |
| 132 | |
| 133 | foreach ($data as $row) { |
| 134 | if (isset($row['campaign_id']) && $row['campaign_id'] == $campaign->id) { |
| 135 | return $row['sum']; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @since 4.0.0 |
| 144 | * |
| 145 | * Get donations count for campaign |
| 146 | * |
| 147 | * @param Campaign $campaign |
| 148 | * |
| 149 | * @return int |
| 150 | */ |
| 151 | public function getDonationsCount(Campaign $campaign): int |
| 152 | { |
| 153 | $data = $campaign->goalType->isSubscriptions() |
| 154 | ? $this->subscriptionDonationsCount |
| 155 | : $this->donationsCount; |
| 156 | |
| 157 | foreach ($data as $row) { |
| 158 | if (isset($row['campaign_id']) && $row['campaign_id'] == $campaign->id) { |
| 159 | return (int)$row['count']; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @since 4.0.0 |
| 168 | * |
| 169 | * Get donors count for campaign |
| 170 | * |
| 171 | * @param Campaign $campaign |
| 172 | * |
| 173 | * @return int |
| 174 | */ |
| 175 | public function getDonorsCount(Campaign $campaign): int |
| 176 | { |
| 177 | $data = $campaign->goalType->isSubscriptions() |
| 178 | ? $this->subscriptionDonorsCount |
| 179 | : $this->donorsCount; |
| 180 | |
| 181 | foreach ($data as $row) { |
| 182 | if (isset($row['campaign_id']) && $row['campaign_id'] == $campaign->id) { |
| 183 | return (int)$row['count']; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * @since 4.0.0 |
| 193 | * |
| 194 | * Get goal data for campaign |
| 195 | * |
| 196 | * @param Campaign $campaign |
| 197 | * |
| 198 | * @return array{actual: int, goal: int, actualFormatted: string, goalFormatted:string, percentage:float} |
| 199 | */ |
| 200 | public function getGoalData(Campaign $campaign): array |
| 201 | { |
| 202 | $actual = $this->getActualGoal($campaign); |
| 203 | $percentage = $campaign->goal |
| 204 | ? $actual / $campaign->goal |
| 205 | : 0; |
| 206 | |
| 207 | return [ |
| 208 | 'actual' => $actual, |
| 209 | 'goal' => $campaign->goal, |
| 210 | 'actualFormatted' => $campaign->goalType == CampaignGoalType::AMOUNT |
| 211 | ? give_currency_filter(give_format_amount($actual)) |
| 212 | : $actual, |
| 213 | 'goalFormatted' => $campaign->goalType == CampaignGoalType::AMOUNT |
| 214 | ? give_currency_filter(give_format_amount($campaign->goal)) |
| 215 | : $campaign->goal, |
| 216 | 'percentage' => round($percentage * 100, 2), |
| 217 | ]; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @since 4.2.0 return union type int|float |
| 222 | * @since 4.0.0 |
| 223 | * |
| 224 | * @param Campaign $campaign |
| 225 | * |
| 226 | * @return int|float |
| 227 | */ |
| 228 | private function getActualGoal(Campaign $campaign) |
| 229 | { |
| 230 | switch ($campaign->goalType->getValue()) { |
| 231 | case CampaignGoalType::DONATIONS(): |
| 232 | case CampaignGoalType::SUBSCRIPTIONS(): |
| 233 | return $this->getDonationsCount($campaign); |
| 234 | case CampaignGoalType::DONORS(): |
| 235 | case CampaignGoalType::DONORS_FROM_SUBSCRIPTIONS(): |
| 236 | return $this->getDonorsCount($campaign); |
| 237 | default: |
| 238 | return $this->getRevenue($campaign); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 |