Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phortune/query/PhortuneSubscriptionQuery.php
<?php | <?php | ||||
final class PhortuneSubscriptionQuery | final class PhortuneSubscriptionQuery | ||||
extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
private $ids; | private $ids; | ||||
private $phids; | private $phids; | ||||
private $accountPHIDs; | private $accountPHIDs; | ||||
private $merchantPHIDs; | private $merchantPHIDs; | ||||
private $statuses; | private $statuses; | ||||
private $paymentMethodPHIDs; | |||||
private $needTriggers; | private $needTriggers; | ||||
public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
$this->ids = $ids; | $this->ids = $ids; | ||||
return $this; | return $this; | ||||
} | } | ||||
Show All 12 Lines | public function withMerchantPHIDs(array $merchant_phids) { | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withStatuses(array $statuses) { | public function withStatuses(array $statuses) { | ||||
$this->statuses = $statuses; | $this->statuses = $statuses; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withPaymentMethodPHIDs(array $method_phids) { | |||||
$this->paymentMethodPHIDs = $method_phids; | |||||
return $this; | |||||
} | |||||
public function needTriggers($need_triggers) { | public function needTriggers($need_triggers) { | ||||
$this->needTriggers = $need_triggers; | $this->needTriggers = $need_triggers; | ||||
return $this; | return $this; | ||||
} | } | ||||
protected function loadPage() { | public function newResultObject() { | ||||
$table = new PhortuneSubscription(); | return new PhortuneSubscription(); | ||||
$conn = $table->establishConnection('r'); | } | ||||
$rows = queryfx_all( | |||||
$conn, | |||||
'SELECT subscription.* FROM %T subscription %Q %Q %Q', | |||||
$table->getTableName(), | |||||
$this->buildWhereClause($conn), | |||||
$this->buildOrderClause($conn), | |||||
$this->buildLimitClause($conn)); | |||||
return $table->loadAllFromArray($rows); | protected function loadPage() { | ||||
return $this->loadStandardPage($this->newResultObject()); | |||||
} | } | ||||
protected function willFilterPage(array $subscriptions) { | protected function willFilterPage(array $subscriptions) { | ||||
$accounts = id(new PhortuneAccountQuery()) | $accounts = id(new PhortuneAccountQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
->withPHIDs(mpull($subscriptions, 'getAccountPHID')) | ->withPHIDs(mpull($subscriptions, 'getAccountPHID')) | ||||
->execute(); | ->execute(); | ||||
$accounts = mpull($accounts, null, 'getPHID'); | $accounts = mpull($accounts, null, 'getPHID'); | ||||
foreach ($subscriptions as $key => $subscription) { | foreach ($subscriptions as $key => $subscription) { | ||||
$account = idx($accounts, $subscription->getAccountPHID()); | $account = idx($accounts, $subscription->getAccountPHID()); | ||||
if (!$account) { | if (!$account) { | ||||
unset($subscriptions[$key]); | unset($subscriptions[$key]); | ||||
$this->didRejectResult($subscription); | |||||
continue; | continue; | ||||
} | } | ||||
$subscription->attachAccount($account); | $subscription->attachAccount($account); | ||||
} | } | ||||
if (!$subscriptions) { | if (!$subscriptions) { | ||||
return $subscriptions; | return $subscriptions; | ||||
} | } | ||||
$merchants = id(new PhortuneMerchantQuery()) | $merchants = id(new PhortuneMerchantQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
->withPHIDs(mpull($subscriptions, 'getMerchantPHID')) | ->withPHIDs(mpull($subscriptions, 'getMerchantPHID')) | ||||
->execute(); | ->execute(); | ||||
$merchants = mpull($merchants, null, 'getPHID'); | $merchants = mpull($merchants, null, 'getPHID'); | ||||
foreach ($subscriptions as $key => $subscription) { | foreach ($subscriptions as $key => $subscription) { | ||||
$merchant = idx($merchants, $subscription->getMerchantPHID()); | $merchant = idx($merchants, $subscription->getMerchantPHID()); | ||||
if (!$merchant) { | if (!$merchant) { | ||||
unset($subscriptions[$key]); | unset($subscriptions[$key]); | ||||
$this->didRejectResult($subscription); | |||||
continue; | continue; | ||||
} | } | ||||
$subscription->attachMerchant($merchant); | $subscription->attachMerchant($merchant); | ||||
} | } | ||||
if (!$subscriptions) { | if (!$subscriptions) { | ||||
return $subscriptions; | return $subscriptions; | ||||
} | } | ||||
Show All 10 Lines | foreach ($subscription_map as $class => $class_subscriptions) { | ||||
$implementations += mpull($impl_objects, null, 'getRef'); | $implementations += mpull($impl_objects, null, 'getRef'); | ||||
} | } | ||||
foreach ($subscriptions as $key => $subscription) { | foreach ($subscriptions as $key => $subscription) { | ||||
$ref = $subscription->getSubscriptionRef(); | $ref = $subscription->getSubscriptionRef(); | ||||
$implementation = idx($implementations, $ref); | $implementation = idx($implementations, $ref); | ||||
if (!$implementation) { | if (!$implementation) { | ||||
unset($subscriptions[$key]); | unset($subscriptions[$key]); | ||||
$this->didRejectResult($subscription); | |||||
continue; | continue; | ||||
} | } | ||||
$subscription->attachImplementation($implementation); | $subscription->attachImplementation($implementation); | ||||
} | } | ||||
if (!$subscriptions) { | if (!$subscriptions) { | ||||
return $subscriptions; | return $subscriptions; | ||||
} | } | ||||
if ($this->needTriggers) { | if ($this->needTriggers) { | ||||
$trigger_phids = mpull($subscriptions, 'getTriggerPHID'); | $trigger_phids = mpull($subscriptions, 'getTriggerPHID'); | ||||
$triggers = id(new PhabricatorWorkerTriggerQuery()) | $triggers = id(new PhabricatorWorkerTriggerQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
->withPHIDs($trigger_phids) | ->withPHIDs($trigger_phids) | ||||
->needEvents(true) | ->needEvents(true) | ||||
->execute(); | ->execute(); | ||||
$triggers = mpull($triggers, null, 'getPHID'); | $triggers = mpull($triggers, null, 'getPHID'); | ||||
foreach ($subscriptions as $key => $subscription) { | foreach ($subscriptions as $key => $subscription) { | ||||
$trigger = idx($triggers, $subscription->getTriggerPHID()); | $trigger = idx($triggers, $subscription->getTriggerPHID()); | ||||
if (!$trigger) { | if (!$trigger) { | ||||
unset($subscriptions[$key]); | unset($subscriptions[$key]); | ||||
$this->didRejectResult($subscription); | |||||
continue; | continue; | ||||
} | } | ||||
$subscription->attachTrigger($trigger); | $subscription->attachTrigger($trigger); | ||||
} | } | ||||
} | } | ||||
return $subscriptions; | return $subscriptions; | ||||
} | } | ||||
protected function buildWhereClause(AphrontDatabaseConnection $conn) { | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
$where = array(); | $where = parent::buildWhereClauseParts($conn); | ||||
$where[] = $this->buildPagingClause($conn); | |||||
if ($this->ids !== null) { | if ($this->ids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'subscription.id IN (%Ld)', | 'subscription.id IN (%Ld)', | ||||
$this->ids); | $this->ids); | ||||
} | } | ||||
Show All 20 Lines | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
if ($this->statuses !== null) { | if ($this->statuses !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'subscription.status IN (%Ls)', | 'subscription.status IN (%Ls)', | ||||
$this->statuses); | $this->statuses); | ||||
} | } | ||||
return $this->formatWhereClause($conn, $where); | if ($this->paymentMethodPHIDs !== null) { | ||||
$where[] = qsprintf( | |||||
$conn, | |||||
'subscription.defaultPaymentMethodPHID IN (%Ls)', | |||||
$this->paymentMethodPHIDs); | |||||
} | |||||
return $where; | |||||
} | |||||
protected function getPrimaryTableAlias() { | |||||
return 'subscription'; | |||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorPhortuneApplication'; | return 'PhabricatorPhortuneApplication'; | ||||
} | } | ||||
} | } |