From 4adc2d8a72fce0eb23a2116be3cc93497f488c43 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 30 Jan 2015 11:28:13 -0800 Subject: [PATCH] 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 --- .../PhortuneAccountViewController.php | 3 + .../query/PhortuneSubscriptionQuery.php | 9 +- .../PhortuneSubscriptionSearchEngine.php | 1 + .../phortune/storage/PhortuneSubscription.php | 90 ++++++++++++++++++- 4 files changed, 97 insertions(+), 6 deletions(-) diff --git a/src/applications/phortune/controller/PhortuneAccountViewController.php b/src/applications/phortune/controller/PhortuneAccountViewController.php index 59dc2b76f6..0bd99bd39b 100644 --- a/src/applications/phortune/controller/PhortuneAccountViewController.php +++ b/src/applications/phortune/controller/PhortuneAccountViewController.php @@ -275,8 +275,11 @@ final class PhortuneAccountViewController extends PhortuneController { $subscriptions_uri = $this->getApplicationURI( $account->getID().'/subscription/'); + $handles = $this->loadViewerHandles(mpull($subscriptions, 'getPHID')); + $table = id(new PhortuneSubscriptionTableView()) ->setUser($viewer) + ->setHandles($handles) ->setSubscriptions($subscriptions); $header = id(new PHUIHeaderView()) diff --git a/src/applications/phortune/query/PhortuneSubscriptionQuery.php b/src/applications/phortune/query/PhortuneSubscriptionQuery.php index 3d718df4f5..91c3f07034 100644 --- a/src/applications/phortune/query/PhortuneSubscriptionQuery.php +++ b/src/applications/phortune/query/PhortuneSubscriptionQuery.php @@ -85,13 +85,16 @@ final class PhortuneSubscriptionQuery $subscription_map = mgroup($subscriptions, 'getSubscriptionClass'); foreach ($subscription_map as $class => $class_subscriptions) { $sub = newv($class, array()); - $implementations += $sub->loadImplementationsForSubscriptions( + $impl_objects = $sub->loadImplementationsForRefs( $this->getViewer(), - $class_subscriptions); + mpull($class_subscriptions, 'getSubscriptionRef')); + + $implementations += mpull($impl_objects, null, 'getRef'); } foreach ($subscriptions as $key => $subscription) { - $implementation = idx($implementations, $key); + $ref = $subscription->getSubscriptionRef(); + $implementation = idx($implementations, $ref); if (!$implementation) { unset($subscriptions[$key]); continue; diff --git a/src/applications/phortune/query/PhortuneSubscriptionSearchEngine.php b/src/applications/phortune/query/PhortuneSubscriptionSearchEngine.php index d8009d8cf4..5baa6acc45 100644 --- a/src/applications/phortune/query/PhortuneSubscriptionSearchEngine.php +++ b/src/applications/phortune/query/PhortuneSubscriptionSearchEngine.php @@ -138,6 +138,7 @@ final class PhortuneSubscriptionSearchEngine $table = id(new PhortuneSubscriptionTableView()) ->setUser($viewer) + ->setHandles($handles) ->setSubscriptions($subscriptions); $merchant = $this->getMerchant(); diff --git a/src/applications/phortune/storage/PhortuneSubscription.php b/src/applications/phortune/storage/PhortuneSubscription.php index 70bf6de409..498ef2f9ea 100644 --- a/src/applications/phortune/storage/PhortuneSubscription.php +++ b/src/applications/phortune/storage/PhortuneSubscription.php @@ -58,19 +58,66 @@ final class PhortuneSubscription extends PhortuneDAO PhortuneSubscriptionPHIDType::TYPECONST); } - public static function initializeNewSubscription() { - return id(new PhortuneSubscription()); + public static function initializeNewSubscription( + 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( PhortuneSubscriptionImplementation $impl) { $this->implementation = $impl; + return $this; } public function getImplementation() { 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() { $this->subscriptionClassKey = PhabricatorHash::digestForIndex( $this->subscriptionClass); @@ -78,7 +125,44 @@ final class PhortuneSubscription extends PhortuneDAO $this->subscriptionRefKey = PhabricatorHash::digestForIndex( $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); }