1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-30 01:10:58 +01:00

Make it possible for applications to generate concrete subscriptions

Summary:
Ref T6881. This still doesn't "work" in any reasonable sense of the word, but gets us a bit further.

I'll build out the Phortune UI a little bit next, then look at implementing the Worker to do actual billing.

Test Plan:
  - Allocated an instance and saw a Subscription generate properly.
  - Saw subscription show up in the Phortune UI, albeit in a very limited way.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T6881

Differential Revision: https://secure.phabricator.com/D11575
This commit is contained in:
epriestley 2015-01-30 11:28:13 -08:00
parent 77eae81e1a
commit 4adc2d8a72
4 changed files with 97 additions and 6 deletions

View file

@ -275,8 +275,11 @@ final class PhortuneAccountViewController extends PhortuneController {
$subscriptions_uri = $this->getApplicationURI( $subscriptions_uri = $this->getApplicationURI(
$account->getID().'/subscription/'); $account->getID().'/subscription/');
$handles = $this->loadViewerHandles(mpull($subscriptions, 'getPHID'));
$table = id(new PhortuneSubscriptionTableView()) $table = id(new PhortuneSubscriptionTableView())
->setUser($viewer) ->setUser($viewer)
->setHandles($handles)
->setSubscriptions($subscriptions); ->setSubscriptions($subscriptions);
$header = id(new PHUIHeaderView()) $header = id(new PHUIHeaderView())

View file

@ -85,13 +85,16 @@ final class PhortuneSubscriptionQuery
$subscription_map = mgroup($subscriptions, 'getSubscriptionClass'); $subscription_map = mgroup($subscriptions, 'getSubscriptionClass');
foreach ($subscription_map as $class => $class_subscriptions) { foreach ($subscription_map as $class => $class_subscriptions) {
$sub = newv($class, array()); $sub = newv($class, array());
$implementations += $sub->loadImplementationsForSubscriptions( $impl_objects = $sub->loadImplementationsForRefs(
$this->getViewer(), $this->getViewer(),
$class_subscriptions); mpull($class_subscriptions, 'getSubscriptionRef'));
$implementations += mpull($impl_objects, null, 'getRef');
} }
foreach ($subscriptions as $key => $subscription) { foreach ($subscriptions as $key => $subscription) {
$implementation = idx($implementations, $key); $ref = $subscription->getSubscriptionRef();
$implementation = idx($implementations, $ref);
if (!$implementation) { if (!$implementation) {
unset($subscriptions[$key]); unset($subscriptions[$key]);
continue; continue;

View file

@ -138,6 +138,7 @@ final class PhortuneSubscriptionSearchEngine
$table = id(new PhortuneSubscriptionTableView()) $table = id(new PhortuneSubscriptionTableView())
->setUser($viewer) ->setUser($viewer)
->setHandles($handles)
->setSubscriptions($subscriptions); ->setSubscriptions($subscriptions);
$merchant = $this->getMerchant(); $merchant = $this->getMerchant();

View file

@ -58,19 +58,66 @@ final class PhortuneSubscription extends PhortuneDAO
PhortuneSubscriptionPHIDType::TYPECONST); PhortuneSubscriptionPHIDType::TYPECONST);
} }
public static function initializeNewSubscription() { public static function initializeNewSubscription(
return id(new PhortuneSubscription()); PhortuneAccount $account,
PhortuneMerchant $merchant,
PhabricatorUser $author,
PhortuneSubscriptionImplementation $implementation,
PhabricatorTriggerClock $clock) {
$trigger = id(new PhabricatorWorkerTrigger())
->setClock($clock);
return id(new PhortuneSubscription())
->setStatus(self::STATUS_ACTIVE)
->setAccountPHID($account->getPHID())
->attachAccount($account)
->setMerchantPHID($merchant->getPHID())
->attachMerchant($merchant)
->setAuthorPHID($author->getPHID())
->setSubscriptionClass(get_class($implementation))
->setSubscriptionRef($implementation->getRef())
->attachImplementation($implementation)
->attachTrigger($trigger);
} }
public function attachImplementation( public function attachImplementation(
PhortuneSubscriptionImplementation $impl) { PhortuneSubscriptionImplementation $impl) {
$this->implementation = $impl; $this->implementation = $impl;
return $this;
} }
public function getImplementation() { public function getImplementation() {
return $this->assertAttached($this->implementation); return $this->assertAttached($this->implementation);
} }
public function attachAccount(PhortuneAccount $account) {
$this->account = $account;
return $this;
}
public function getAccount() {
return $this->assertAttached($this->account);
}
public function attachMerchant(PhortuneMerchant $merchant) {
$this->merchant = $merchant;
return $this;
}
public function getMerchant() {
return $this->assertAttached($this->merchant);
}
public function attachTrigger(PhabricatorWorkerTrigger $trigger) {
$this->trigger = $trigger;
return $this;
}
public function getTrigger() {
return $this->assertAttached($this->trigger);
}
public function save() { public function save() {
$this->subscriptionClassKey = PhabricatorHash::digestForIndex( $this->subscriptionClassKey = PhabricatorHash::digestForIndex(
$this->subscriptionClass); $this->subscriptionClass);
@ -78,7 +125,44 @@ final class PhortuneSubscription extends PhortuneDAO
$this->subscriptionRefKey = PhabricatorHash::digestForIndex( $this->subscriptionRefKey = PhabricatorHash::digestForIndex(
$this->subscriptionRef); $this->subscriptionRef);
return parent::save(); $trigger = $this->getTrigger();
$is_new = (!$this->getID());
$this->openTransaction();
// If we're saving this subscription for the first time, we're also
// going to set up the trigger for it.
if ($is_new) {
$trigger_phid = PhabricatorPHID::generateNewPHID(
PhabricatorWorkerTriggerPHIDType::TYPECONST);
$this->setTriggerPHID($trigger_phid);
}
$result = parent::save();
if ($is_new) {
$trigger_action = new PhabricatorScheduleTaskTriggerAction(
array(
'class' => 'PhortuneSubscriptionWorker',
'data' => array(
'subscriptionPHID' => $this->getPHID(),
),
'options' => array(
'objectPHID' => $this->getPHID(),
'priority' => PhabricatorWorker::PRIORITY_BULK,
),
));
$trigger->setAction($trigger_action);
$trigger->save();
}
$this->saveTransaction();
return $result;
}
public function getSubscriptionName() {
return $this->getImplementation()->getName($this);
} }