Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / nuance / command / NuanceCommandImplementation.php
blob2b47e40654a3c3cfb747c11938bd86d84d78acff
1 <?php
3 abstract class NuanceCommandImplementation
4 extends Phobject {
6 private $actor;
8 private $transactionQueue = array();
10 final public function setActor(PhabricatorUser $actor) {
11 $this->actor = $actor;
12 return $this;
15 final public function getActor() {
16 return $this->actor;
19 abstract public function getCommandName();
20 abstract public function canApplyToItem(NuanceItem $item);
22 public function canApplyImmediately(
23 NuanceItem $item,
24 NuanceItemCommand $command) {
25 return false;
28 abstract protected function executeCommand(
29 NuanceItem $item,
30 NuanceItemCommand $command);
32 final public function applyCommand(
33 NuanceItem $item,
34 NuanceItemCommand $command) {
36 $command_key = $command->getCommand();
37 $implementation_key = $this->getCommandKey();
38 if ($command_key !== $implementation_key) {
39 throw new Exception(
40 pht(
41 'This command implementation("%s") can not apply a command of a '.
42 'different type ("%s").',
43 $implementation_key,
44 $command_key));
47 if (!$this->canApplyToItem($item)) {
48 throw new Exception(
49 pht(
50 'This command implementation ("%s") can not be applied to an '.
51 'item of type "%s".',
52 $implementation_key,
53 $item->getItemType()));
56 $this->transactionQueue = array();
58 $command_type = NuanceItemCommandTransaction::TRANSACTIONTYPE;
59 $command_xaction = $this->newTransaction($command_type);
61 $result = $this->executeCommand($item, $command);
63 $xactions = $this->transactionQueue;
64 $this->transactionQueue = array();
66 $command_xaction->setNewValue(
67 array(
68 'command' => $command->getCommand(),
69 'parameters' => $command->getParameters(),
70 'result' => $result,
71 ));
73 // TODO: Maybe preserve the actor's original content source?
74 $source = PhabricatorContentSource::newForSource(
75 PhabricatorDaemonContentSource::SOURCECONST);
77 $actor = $this->getActor();
79 id(new NuanceItemEditor())
80 ->setActor($actor)
81 ->setActingAsPHID($command->getAuthorPHID())
82 ->setContentSource($source)
83 ->setContinueOnMissingFields(true)
84 ->setContinueOnNoEffect(true)
85 ->applyTransactions($item, $xactions);
88 final public function getCommandKey() {
89 return $this->getPhobjectClassConstant('COMMANDKEY');
92 final public static function getAllCommands() {
93 return id(new PhutilClassMapQuery())
94 ->setAncestorClass(__CLASS__)
95 ->setUniqueMethod('getCommandKey')
96 ->execute();
99 protected function newTransaction($type) {
100 $xaction = id(new NuanceItemTransaction())
101 ->setTransactionType($type);
103 $this->transactionQueue[] = $xaction;
105 return $xaction;
108 protected function newStatusTransaction($status) {
109 return $this->newTransaction(NuanceItemStatusTransaction::TRANSACTIONTYPE)
110 ->setNewValue($status);