Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / almanac / xaction / AlmanacBindingInterfaceTransaction.php
blob03effc028e235f67be067b177779d8155d78b72c
1 <?php
3 final class AlmanacBindingInterfaceTransaction
4 extends AlmanacBindingTransactionType {
6 const TRANSACTIONTYPE = 'almanac:binding:interface';
8 public function generateOldValue($object) {
9 return $object->getInterfacePHID();
12 public function applyInternalEffects($object, $value) {
13 $interface = $this->loadInterface($value);
15 $object
16 ->setDevicePHID($interface->getDevicePHID())
17 ->setInterfacePHID($interface->getPHID());
20 public function applyExternalEffects($object, $value) {
22 // When we change which services a device is bound to, we need to
23 // recalculate whether it is a cluster device or not so we can tell if
24 // the "Can Manage Cluster Services" permission applies to it.
26 $viewer = PhabricatorUser::getOmnipotentUser();
27 $interface_phids = array();
29 $interface_phids[] = $this->getOldValue();
30 $interface_phids[] = $this->getNewValue();
32 $interface_phids = array_filter($interface_phids);
33 $interface_phids = array_unique($interface_phids);
35 $interfaces = id(new AlmanacInterfaceQuery())
36 ->setViewer($viewer)
37 ->withPHIDs($interface_phids)
38 ->execute();
40 $device_phids = array();
41 foreach ($interfaces as $interface) {
42 $device_phids[] = $interface->getDevicePHID();
45 $device_phids = array_unique($device_phids);
47 $devices = id(new AlmanacDeviceQuery())
48 ->setViewer($viewer)
49 ->withPHIDs($device_phids)
50 ->execute();
52 foreach ($devices as $device) {
53 $device->rebuildClusterBindingStatus();
57 public function getTitle() {
58 if ($this->getOldValue() === null) {
59 return pht(
60 '%s set the interface for this binding to %s.',
61 $this->renderAuthor(),
62 $this->renderNewHandle());
63 } else if ($this->getNewValue() == null) {
64 return pht(
65 '%s removed the interface for this binding.',
66 $this->renderAuthor());
67 } else {
68 return pht(
69 '%s changed the interface for this binding from %s to %s.',
70 $this->renderAuthor(),
71 $this->renderOldHandle(),
72 $this->renderNewHandle());
76 public function validateTransactions($object, array $xactions) {
77 $errors = array();
79 $interface_phid = $object->getInterfacePHID();
80 if ($this->isEmptyTextTransaction($interface_phid, $xactions)) {
81 $errors[] = $this->newRequiredError(
82 pht('Bindings must specify an interface.'));
85 foreach ($xactions as $xaction) {
86 $interface_phid = $xaction->getNewValue();
88 $interface = $this->loadInterface($interface_phid);
89 if (!$interface) {
90 $errors[] = $this->newInvalidError(
91 pht(
92 'You can not bind a service to an invalid or restricted '.
93 'interface.'),
94 $xaction);
95 continue;
98 $binding = id(new AlmanacBindingQuery())
99 ->setViewer(PhabricatorUser::getOmnipotentUser())
100 ->withServicePHIDs(array($object->getServicePHID()))
101 ->withInterfacePHIDs(array($interface_phid))
102 ->executeOne();
103 if ($binding && ($binding->getID() != $object->getID())) {
104 $errors[] = $this->newInvalidError(
105 pht(
106 'You can not bind a service to the same interface multiple '.
107 'times.'),
108 $xaction);
109 continue;
113 return $errors;
116 private function loadInterface($phid) {
117 return id(new AlmanacInterfaceQuery())
118 ->setViewer($this->getActor())
119 ->withPHIDs(array($phid))
120 ->executeOne();