@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 200 lines 5.2 kB view raw
1<?php 2 3final class PhortuneSubscriptionQuery 4 extends PhabricatorCursorPagedPolicyAwareQuery { 5 6 private $ids; 7 private $phids; 8 private $accountPHIDs; 9 private $merchantPHIDs; 10 private $statuses; 11 private $paymentMethodPHIDs; 12 13 private $needTriggers; 14 15 public function withIDs(array $ids) { 16 $this->ids = $ids; 17 return $this; 18 } 19 20 public function withPHIDs(array $phids) { 21 $this->phids = $phids; 22 return $this; 23 } 24 25 public function withAccountPHIDs(array $account_phids) { 26 $this->accountPHIDs = $account_phids; 27 return $this; 28 } 29 30 public function withMerchantPHIDs(array $merchant_phids) { 31 $this->merchantPHIDs = $merchant_phids; 32 return $this; 33 } 34 35 public function withStatuses(array $statuses) { 36 $this->statuses = $statuses; 37 return $this; 38 } 39 40 public function withPaymentMethodPHIDs(array $method_phids) { 41 $this->paymentMethodPHIDs = $method_phids; 42 return $this; 43 } 44 45 public function needTriggers($need_triggers) { 46 $this->needTriggers = $need_triggers; 47 return $this; 48 } 49 50 public function newResultObject() { 51 return new PhortuneSubscription(); 52 } 53 54 protected function willFilterPage(array $subscriptions) { 55 $accounts = id(new PhortuneAccountQuery()) 56 ->setViewer($this->getViewer()) 57 ->withPHIDs(mpull($subscriptions, 'getAccountPHID')) 58 ->execute(); 59 $accounts = mpull($accounts, null, 'getPHID'); 60 61 foreach ($subscriptions as $key => $subscription) { 62 $account = idx($accounts, $subscription->getAccountPHID()); 63 if (!$account) { 64 unset($subscriptions[$key]); 65 $this->didRejectResult($subscription); 66 continue; 67 } 68 $subscription->attachAccount($account); 69 } 70 71 if (!$subscriptions) { 72 return $subscriptions; 73 } 74 75 $merchants = id(new PhortuneMerchantQuery()) 76 ->setViewer($this->getViewer()) 77 ->withPHIDs(mpull($subscriptions, 'getMerchantPHID')) 78 ->execute(); 79 $merchants = mpull($merchants, null, 'getPHID'); 80 81 foreach ($subscriptions as $key => $subscription) { 82 $merchant = idx($merchants, $subscription->getMerchantPHID()); 83 if (!$merchant) { 84 unset($subscriptions[$key]); 85 $this->didRejectResult($subscription); 86 continue; 87 } 88 $subscription->attachMerchant($merchant); 89 } 90 91 if (!$subscriptions) { 92 return $subscriptions; 93 } 94 95 $implementations = array(); 96 97 $subscription_map = mgroup($subscriptions, 'getSubscriptionClass'); 98 foreach ($subscription_map as $class => $class_subscriptions) { 99 $sub = newv($class, array()); 100 $impl_objects = $sub->loadImplementationsForRefs( 101 $this->getViewer(), 102 mpull($class_subscriptions, 'getSubscriptionRef')); 103 104 $implementations += mpull($impl_objects, null, 'getRef'); 105 } 106 107 foreach ($subscriptions as $key => $subscription) { 108 $ref = $subscription->getSubscriptionRef(); 109 $implementation = idx($implementations, $ref); 110 if (!$implementation) { 111 unset($subscriptions[$key]); 112 $this->didRejectResult($subscription); 113 continue; 114 } 115 $subscription->attachImplementation($implementation); 116 } 117 118 if (!$subscriptions) { 119 return $subscriptions; 120 } 121 122 if ($this->needTriggers) { 123 $trigger_phids = mpull($subscriptions, 'getTriggerPHID'); 124 $triggers = id(new PhabricatorWorkerTriggerQuery()) 125 ->setViewer($this->getViewer()) 126 ->withPHIDs($trigger_phids) 127 ->needEvents(true) 128 ->execute(); 129 $triggers = mpull($triggers, null, 'getPHID'); 130 foreach ($subscriptions as $key => $subscription) { 131 $trigger = idx($triggers, $subscription->getTriggerPHID()); 132 if (!$trigger) { 133 unset($subscriptions[$key]); 134 $this->didRejectResult($subscription); 135 continue; 136 } 137 $subscription->attachTrigger($trigger); 138 } 139 } 140 141 return $subscriptions; 142 } 143 144 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 145 $where = parent::buildWhereClauseParts($conn); 146 147 if ($this->ids !== null) { 148 $where[] = qsprintf( 149 $conn, 150 'subscription.id IN (%Ld)', 151 $this->ids); 152 } 153 154 if ($this->phids !== null) { 155 $where[] = qsprintf( 156 $conn, 157 'subscription.phid IN (%Ls)', 158 $this->phids); 159 } 160 161 if ($this->accountPHIDs !== null) { 162 $where[] = qsprintf( 163 $conn, 164 'subscription.accountPHID IN (%Ls)', 165 $this->accountPHIDs); 166 } 167 168 if ($this->merchantPHIDs !== null) { 169 $where[] = qsprintf( 170 $conn, 171 'subscription.merchantPHID IN (%Ls)', 172 $this->merchantPHIDs); 173 } 174 175 if ($this->statuses !== null) { 176 $where[] = qsprintf( 177 $conn, 178 'subscription.status IN (%Ls)', 179 $this->statuses); 180 } 181 182 if ($this->paymentMethodPHIDs !== null) { 183 $where[] = qsprintf( 184 $conn, 185 'subscription.defaultPaymentMethodPHID IN (%Ls)', 186 $this->paymentMethodPHIDs); 187 } 188 189 return $where; 190 } 191 192 protected function getPrimaryTableAlias() { 193 return 'subscription'; 194 } 195 196 public function getQueryApplicationClass() { 197 return PhabricatorPhortuneApplication::class; 198 } 199 200}