Path: blob/master/src/applications/nuance/source/NuanceSourceDefinition.php
12256 views
<?php12/**3* @task action Handling Action Requests4*/5abstract class NuanceSourceDefinition extends Phobject {67private $viewer;8private $source;910public function setViewer(PhabricatorUser $viewer) {11$this->viewer = $viewer;12return $this;13}1415public function getViewer() {16if (!$this->viewer) {17throw new PhutilInvalidStateException('setViewer');18}19return $this->viewer;20}2122public function setSource(NuanceSource $source) {23$this->source = $source;24return $this;25}2627public function getSource() {28if (!$this->source) {29throw new PhutilInvalidStateException('setSource');30}31return $this->source;32}3334public function getSourceViewActions(AphrontRequest $request) {35return array();36}3738public static function getAllDefinitions() {39return id(new PhutilClassMapQuery())40->setAncestorClass(__CLASS__)41->setUniqueMethod('getSourceTypeConstant')42->execute();43}4445public function hasImportCursors() {46return false;47}4849final public function getImportCursors() {50if (!$this->hasImportCursors()) {51throw new Exception(52pht('This source has no input cursors.'));53}5455$viewer = PhabricatorUser::getOmnipotentUser();56$source = $this->getSource();57$cursors = $this->newImportCursors();5859$data = id(new NuanceImportCursorDataQuery())60->setViewer($viewer)61->withSourcePHIDs(array($source->getPHID()))62->execute();63$data = mpull($data, null, 'getCursorKey');6465$map = array();66foreach ($cursors as $cursor) {67if (!($cursor instanceof NuanceImportCursor)) {68throw new Exception(69pht(70'Source "%s" (of class "%s") returned an invalid value from '.71'method "%s": all values must be objects of class "%s".',72$this->getName(),73get_class($this),74'newImportCursors()',75'NuanceImportCursor'));76}7778$key = $cursor->getCursorKey();79if (!strlen($key)) {80throw new Exception(81pht(82'Source "%s" (of class "%s") returned an import cursor with '.83'a missing key from "%s". Each cursor must have a unique, '.84'nonempty key.',85$this->getName(),86get_class($this),87'newImportCursors()'));88}8990$other = idx($map, $key);91if ($other) {92throw new Exception(93pht(94'Source "%s" (of class "%s") returned two cursors from method '.95'"%s" with the same key ("%s"). Each cursor must have a unique '.96'key.',97$this->getName(),98get_class($this),99'newImportCursors()',100$key));101}102103$map[$key] = $cursor;104105$cursor_data = idx($data, $key);106if (!$cursor_data) {107$cursor_data = $cursor->newEmptyCursorData($source);108}109110$cursor111->setViewer($viewer)112->setSource($source)113->setCursorData($cursor_data);114}115116return $map;117}118119protected function newImportCursors() {120throw new PhutilMethodNotImplementedException();121}122123/**124* A human readable string like "Twitter" or "Phabricator Form".125*/126abstract public function getName();127128129/**130* Human readable description of this source, a sentence or two long.131*/132abstract public function getSourceDescription();133134/**135* This should be a any VARCHAR(32).136*137* @{method:getAllDefinitions} will throw if you choose a string that138* collides with another @{class:NuanceSourceDefinition} class.139*/140abstract public function getSourceTypeConstant();141142public function renderView() {143return null;144}145146public function renderListView() {147return null;148}149150protected function newItemFromProperties(151$item_type,152$author_phid,153array $properties,154PhabricatorContentSource $content_source) {155156// TODO: Should we have a tighter actor/viewer model? Requestors will157// often have no real user associated with them...158$actor = PhabricatorUser::getOmnipotentUser();159$source = $this->getSource();160161$item = NuanceItem::initializeNewItem($item_type);162163$xactions = array();164165$xactions[] = id(new NuanceItemTransaction())166->setTransactionType(NuanceItemSourceTransaction::TRANSACTIONTYPE)167->setNewValue($source->getPHID());168169// TODO: Eventually, apply real routing rules. For now, just put everything170// in the default queue for the source.171$xactions[] = id(new NuanceItemTransaction())172->setTransactionType(NuanceItemQueueTransaction::TRANSACTIONTYPE)173->setNewValue($source->getDefaultQueuePHID());174175// TODO: Maybe this should all be modular transactions now?176foreach ($properties as $key => $property) {177$xactions[] = id(new NuanceItemTransaction())178->setTransactionType(NuanceItemPropertyTransaction::TRANSACTIONTYPE)179->setMetadataValue(NuanceItemTransaction::PROPERTY_KEY, $key)180->setNewValue($property);181}182183$editor = id(new NuanceItemEditor())184->setActor($actor)185->setActingAsPHID($author_phid)186->setContentSource($content_source);187188$editor->applyTransactions($item, $xactions);189190return $item;191}192193public function renderItemEditProperties(194PhabricatorUser $viewer,195NuanceItem $item,196PHUIPropertyListView $view) {197return;198}199200201/* -( Handling Action Requests )------------------------------------------- */202203204public function handleActionRequest(AphrontRequest $request) {205return new Aphront404Response();206}207208public function getActionURI($path = null) {209$source_id = $this->getSource()->getID();210return '/action/'.$source_id.'/'.ltrim($path, '/');211}212213}214215216