Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / nuance / source / NuanceSourceDefinition.php
blob2c4ebc65e5b47ac1f07a678094124f608c3646f4
1 <?php
3 /**
4 * @task action Handling Action Requests
5 */
6 abstract class NuanceSourceDefinition extends Phobject {
8 private $viewer;
9 private $source;
11 public function setViewer(PhabricatorUser $viewer) {
12 $this->viewer = $viewer;
13 return $this;
16 public function getViewer() {
17 if (!$this->viewer) {
18 throw new PhutilInvalidStateException('setViewer');
20 return $this->viewer;
23 public function setSource(NuanceSource $source) {
24 $this->source = $source;
25 return $this;
28 public function getSource() {
29 if (!$this->source) {
30 throw new PhutilInvalidStateException('setSource');
32 return $this->source;
35 public function getSourceViewActions(AphrontRequest $request) {
36 return array();
39 public static function getAllDefinitions() {
40 return id(new PhutilClassMapQuery())
41 ->setAncestorClass(__CLASS__)
42 ->setUniqueMethod('getSourceTypeConstant')
43 ->execute();
46 public function hasImportCursors() {
47 return false;
50 final public function getImportCursors() {
51 if (!$this->hasImportCursors()) {
52 throw new Exception(
53 pht('This source has no input cursors.'));
56 $viewer = PhabricatorUser::getOmnipotentUser();
57 $source = $this->getSource();
58 $cursors = $this->newImportCursors();
60 $data = id(new NuanceImportCursorDataQuery())
61 ->setViewer($viewer)
62 ->withSourcePHIDs(array($source->getPHID()))
63 ->execute();
64 $data = mpull($data, null, 'getCursorKey');
66 $map = array();
67 foreach ($cursors as $cursor) {
68 if (!($cursor instanceof NuanceImportCursor)) {
69 throw new Exception(
70 pht(
71 'Source "%s" (of class "%s") returned an invalid value from '.
72 'method "%s": all values must be objects of class "%s".',
73 $this->getName(),
74 get_class($this),
75 'newImportCursors()',
76 'NuanceImportCursor'));
79 $key = $cursor->getCursorKey();
80 if (!strlen($key)) {
81 throw new Exception(
82 pht(
83 'Source "%s" (of class "%s") returned an import cursor with '.
84 'a missing key from "%s". Each cursor must have a unique, '.
85 'nonempty key.',
86 $this->getName(),
87 get_class($this),
88 'newImportCursors()'));
91 $other = idx($map, $key);
92 if ($other) {
93 throw new Exception(
94 pht(
95 'Source "%s" (of class "%s") returned two cursors from method '.
96 '"%s" with the same key ("%s"). Each cursor must have a unique '.
97 'key.',
98 $this->getName(),
99 get_class($this),
100 'newImportCursors()',
101 $key));
104 $map[$key] = $cursor;
106 $cursor_data = idx($data, $key);
107 if (!$cursor_data) {
108 $cursor_data = $cursor->newEmptyCursorData($source);
111 $cursor
112 ->setViewer($viewer)
113 ->setSource($source)
114 ->setCursorData($cursor_data);
117 return $map;
120 protected function newImportCursors() {
121 throw new PhutilMethodNotImplementedException();
125 * A human readable string like "Twitter" or "Phabricator Form".
127 abstract public function getName();
131 * Human readable description of this source, a sentence or two long.
133 abstract public function getSourceDescription();
136 * This should be a any VARCHAR(32).
138 * @{method:getAllDefinitions} will throw if you choose a string that
139 * collides with another @{class:NuanceSourceDefinition} class.
141 abstract public function getSourceTypeConstant();
143 public function renderView() {
144 return null;
147 public function renderListView() {
148 return null;
151 protected function newItemFromProperties(
152 $item_type,
153 $author_phid,
154 array $properties,
155 PhabricatorContentSource $content_source) {
157 // TODO: Should we have a tighter actor/viewer model? Requestors will
158 // often have no real user associated with them...
159 $actor = PhabricatorUser::getOmnipotentUser();
160 $source = $this->getSource();
162 $item = NuanceItem::initializeNewItem($item_type);
164 $xactions = array();
166 $xactions[] = id(new NuanceItemTransaction())
167 ->setTransactionType(NuanceItemSourceTransaction::TRANSACTIONTYPE)
168 ->setNewValue($source->getPHID());
170 // TODO: Eventually, apply real routing rules. For now, just put everything
171 // in the default queue for the source.
172 $xactions[] = id(new NuanceItemTransaction())
173 ->setTransactionType(NuanceItemQueueTransaction::TRANSACTIONTYPE)
174 ->setNewValue($source->getDefaultQueuePHID());
176 // TODO: Maybe this should all be modular transactions now?
177 foreach ($properties as $key => $property) {
178 $xactions[] = id(new NuanceItemTransaction())
179 ->setTransactionType(NuanceItemPropertyTransaction::TRANSACTIONTYPE)
180 ->setMetadataValue(NuanceItemTransaction::PROPERTY_KEY, $key)
181 ->setNewValue($property);
184 $editor = id(new NuanceItemEditor())
185 ->setActor($actor)
186 ->setActingAsPHID($author_phid)
187 ->setContentSource($content_source);
189 $editor->applyTransactions($item, $xactions);
191 return $item;
194 public function renderItemEditProperties(
195 PhabricatorUser $viewer,
196 NuanceItem $item,
197 PHUIPropertyListView $view) {
198 return;
202 /* -( Handling Action Requests )------------------------------------------- */
205 public function handleActionRequest(AphrontRequest $request) {
206 return new Aphront404Response();
209 public function getActionURI($path = null) {
210 $source_id = $this->getSource()->getID();
211 return '/action/'.$source_id.'/'.ltrim($path, '/');