[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/storage/ -> PhortunePurchase.php (source)

   1  <?php
   2  
   3  /**
   4   * A purchase represents a user buying something.
   5   */
   6  final class PhortunePurchase extends PhortuneDAO
   7    implements PhabricatorPolicyInterface {
   8  
   9    const STATUS_PENDING      = 'purchase:pending';
  10    const STATUS_PROCESSING   = 'purchase:processing';
  11    const STATUS_ACTIVE       = 'purchase:active';
  12    const STATUS_CANCELED     = 'purchase:canceled';
  13    const STATUS_DELIVERED    = 'purchase:delivered';
  14    const STATUS_FAILED       = 'purchase:failed';
  15  
  16    protected $productPHID;
  17    protected $accountPHID;
  18    protected $authorPHID;
  19    protected $cartPHID;
  20    protected $basePriceAsCurrency;
  21    protected $quantity;
  22    protected $status;
  23    protected $metadata = array();
  24  
  25    private $cart = self::ATTACHABLE;
  26    private $product = self::ATTACHABLE;
  27  
  28    public static function initializeNewPurchase(
  29      PhabricatorUser $actor,
  30      PhortuneProduct $product) {
  31      return id(new PhortunePurchase())
  32        ->setAuthorPHID($actor->getPHID())
  33        ->setProductPHID($product->getPHID())
  34        ->setQuantity(1)
  35        ->setStatus(self::STATUS_PENDING)
  36        ->setBasePriceAsCurrency($product->getPriceAsCurrency());
  37    }
  38  
  39    public function getConfiguration() {
  40      return array(
  41        self::CONFIG_AUX_PHID => true,
  42        self::CONFIG_SERIALIZATION => array(
  43          'metadata' => self::SERIALIZATION_JSON,
  44        ),
  45        self::CONFIG_APPLICATION_SERIALIZERS => array(
  46          'basePriceAsCurrency' => new PhortuneCurrencySerializer(),
  47        ),
  48        self::CONFIG_COLUMN_SCHEMA => array(
  49          'cartPHID' => 'phid?',
  50          'basePriceAsCurrency' => 'text64',
  51          'quantity' => 'uint32',
  52          'status' => 'text32',
  53        ),
  54        self::CONFIG_KEY_SCHEMA => array(
  55          'key_cart' => array(
  56            'columns' => array('cartPHID'),
  57          ),
  58        ),
  59      ) + parent::getConfiguration();
  60    }
  61  
  62    public function generatePHID() {
  63      return PhabricatorPHID::generateNewPHID(
  64        PhortunePurchasePHIDType::TYPECONST);
  65    }
  66  
  67    public function attachCart(PhortuneCart $cart) {
  68      $this->cart = $cart;
  69      return $this;
  70    }
  71  
  72    public function getCart() {
  73      return $this->assertAttached($this->cart);
  74    }
  75  
  76    public function attachProduct(PhortuneProduct $product) {
  77      $this->product = $product;
  78      return $this;
  79    }
  80  
  81    public function getProduct() {
  82      return $this->assertAttached($this->product);
  83    }
  84  
  85    public function getFullDisplayName() {
  86      return $this->getProduct()->getPurchaseName($this);
  87    }
  88  
  89    public function getTotalPriceAsCurrency() {
  90      return $this->getBasePriceAsCurrency();
  91    }
  92  
  93    public function getMetadataValue($key, $default = null) {
  94      return idx($this->metadata, $key, $default);
  95    }
  96  
  97    public function setMetadataValue($key, $value) {
  98      $this->metadata[$key] = $value;
  99      return $this;
 100    }
 101  
 102  
 103  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 104  
 105  
 106    public function getCapabilities() {
 107      return array(
 108        PhabricatorPolicyCapability::CAN_VIEW,
 109        PhabricatorPolicyCapability::CAN_EDIT,
 110      );
 111    }
 112  
 113    public function getPolicy($capability) {
 114      return $this->getCart()->getPolicy($capability);
 115    }
 116  
 117    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 118      return $this->getCart()->hasAutomaticCapability($capability, $viewer);
 119    }
 120  
 121    public function describeAutomaticCapability($capability) {
 122      return pht('Purchases have the policies of their cart.');
 123    }
 124  
 125  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1