[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/almanac/editor/ -> AlmanacBindingEditor.php (source)

   1  <?php
   2  
   3  final class AlmanacBindingEditor
   4    extends PhabricatorApplicationTransactionEditor {
   5  
   6    public function getEditorApplicationClass() {
   7      return 'PhabricatorAlmanacApplication';
   8    }
   9  
  10    public function getEditorObjectsDescription() {
  11      return pht('Almanac Binding');
  12    }
  13  
  14    public function getTransactionTypes() {
  15      $types = parent::getTransactionTypes();
  16  
  17      $types[] = AlmanacBindingTransaction::TYPE_INTERFACE;
  18  
  19      return $types;
  20    }
  21  
  22    protected function getCustomTransactionOldValue(
  23      PhabricatorLiskDAO $object,
  24      PhabricatorApplicationTransaction $xaction) {
  25      switch ($xaction->getTransactionType()) {
  26        case AlmanacBindingTransaction::TYPE_INTERFACE:
  27          return $object->getInterfacePHID();
  28      }
  29  
  30      return parent::getCustomTransactionOldValue($object, $xaction);
  31    }
  32  
  33    protected function getCustomTransactionNewValue(
  34      PhabricatorLiskDAO $object,
  35      PhabricatorApplicationTransaction $xaction) {
  36  
  37      switch ($xaction->getTransactionType()) {
  38        case AlmanacBindingTransaction::TYPE_INTERFACE:
  39          return $xaction->getNewValue();
  40      }
  41  
  42      return parent::getCustomTransactionNewValue($object, $xaction);
  43    }
  44  
  45    protected function applyCustomInternalTransaction(
  46      PhabricatorLiskDAO $object,
  47      PhabricatorApplicationTransaction $xaction) {
  48  
  49      switch ($xaction->getTransactionType()) {
  50        case AlmanacBindingTransaction::TYPE_INTERFACE:
  51          $interface = id(new AlmanacInterfaceQuery())
  52            ->setViewer($this->requireActor())
  53            ->withPHIDs(array($xaction->getNewValue()))
  54            ->executeOne();
  55          $object->setDevicePHID($interface->getDevicePHID());
  56          $object->setInterfacePHID($interface->getPHID());
  57          return;
  58      }
  59  
  60      return parent::applyCustomInternalTransaction($object, $xaction);
  61    }
  62  
  63    protected function applyCustomExternalTransaction(
  64      PhabricatorLiskDAO $object,
  65      PhabricatorApplicationTransaction $xaction) {
  66  
  67      switch ($xaction->getTransactionType()) {
  68        case AlmanacBindingTransaction::TYPE_INTERFACE:
  69          return;
  70      }
  71  
  72      return parent::applyCustomExternalTransaction($object, $xaction);
  73    }
  74  
  75    protected function validateTransaction(
  76      PhabricatorLiskDAO $object,
  77      $type,
  78      array $xactions) {
  79  
  80      $errors = parent::validateTransaction($object, $type, $xactions);
  81  
  82      switch ($type) {
  83        case AlmanacBindingTransaction::TYPE_INTERFACE:
  84          $missing = $this->validateIsEmptyTextField(
  85            $object->getInterfacePHID(),
  86            $xactions);
  87          if ($missing) {
  88            $error = new PhabricatorApplicationTransactionValidationError(
  89              $type,
  90              pht('Required'),
  91              pht('Bindings must specify an interface.'),
  92              nonempty(last($xactions), null));
  93            $error->setIsMissingFieldError(true);
  94            $errors[] = $error;
  95          } else if ($xactions) {
  96            foreach ($xactions as $xaction) {
  97              $interfaces = id(new AlmanacInterfaceQuery())
  98                ->setViewer($this->requireActor())
  99                ->withPHIDs(array($xaction->getNewValue()))
 100                ->execute();
 101              if (!$interfaces) {
 102                $error = new PhabricatorApplicationTransactionValidationError(
 103                  $type,
 104                  pht('Invalid'),
 105                  pht(
 106                    'You can not bind a service to an invalid or restricted '.
 107                    'interface.'),
 108                  $xaction);
 109                $errors[] = $error;
 110              }
 111            }
 112  
 113            $final_value = last($xactions)->getNewValue();
 114  
 115            $binding = id(new AlmanacBindingQuery())
 116              ->setViewer(PhabricatorUser::getOmnipotentUser())
 117              ->withServicePHIDs(array($object->getServicePHID()))
 118              ->withInterfacePHIDs(array($final_value))
 119              ->executeOne();
 120            if ($binding && ($binding->getID() != $object->getID())) {
 121              $error = new PhabricatorApplicationTransactionValidationError(
 122                $type,
 123                pht('Already Bound'),
 124                pht(
 125                  'You can not bind a service to the same interface multiple '.
 126                  'times.'),
 127                last($xactions));
 128              $errors[] = $error;
 129            }
 130          }
 131          break;
 132      }
 133  
 134      return $errors;
 135    }
 136  
 137  
 138  
 139  }


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