Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / harbormaster / xaction / plan / HarbormasterBuildPlanBehaviorTransaction.php
blob7a65eefdfa8040f5683af9530f2d2a9e5ead9261
1 <?php
3 final class HarbormasterBuildPlanBehaviorTransaction
4 extends HarbormasterBuildPlanTransactionType {
6 const TRANSACTIONTYPE = 'behavior';
8 public function generateOldValue($object) {
9 $behavior = $this->getBehavior();
10 return $behavior->getPlanOption($object)->getKey();
13 public function applyInternalEffects($object, $value) {
14 $key = $this->getStorageKey();
15 return $object->setPlanProperty($key, $value);
18 public function getTitle() {
19 $old_value = $this->getOldValue();
20 $new_value = $this->getNewValue();
22 $behavior = $this->getBehavior();
23 if ($behavior) {
24 $behavior_name = $behavior->getName();
26 $options = $behavior->getOptions();
27 if (isset($options[$old_value])) {
28 $old_value = $options[$old_value]->getName();
31 if (isset($options[$new_value])) {
32 $new_value = $options[$new_value]->getName();
34 } else {
35 $behavior_name = $this->getBehaviorKey();
38 return pht(
39 '%s changed the %s behavior for this plan from %s to %s.',
40 $this->renderAuthor(),
41 $this->renderValue($behavior_name),
42 $this->renderValue($old_value),
43 $this->renderValue($new_value));
46 public function validateTransactions($object, array $xactions) {
47 $errors = array();
49 $behaviors = HarbormasterBuildPlanBehavior::newPlanBehaviors();
50 $behaviors = mpull($behaviors, null, 'getKey');
52 foreach ($xactions as $xaction) {
53 $key = $this->getBehaviorKeyForTransaction($xaction);
55 if (!isset($behaviors[$key])) {
56 $errors[] = $this->newInvalidError(
57 pht(
58 'No behavior with key "%s" exists. Valid keys are: %s.',
59 $key,
60 implode(', ', array_keys($behaviors))),
61 $xaction);
62 continue;
65 $behavior = $behaviors[$key];
66 $options = $behavior->getOptions();
68 $storage_key = HarbormasterBuildPlanBehavior::getStorageKeyForBehaviorKey(
69 $key);
70 $old = $object->getPlanProperty($storage_key);
71 $new = $xaction->getNewValue();
73 if ($old === $new) {
74 continue;
77 if (!isset($options[$new])) {
78 $errors[] = $this->newInvalidError(
79 pht(
80 'Value "%s" is not a valid option for behavior "%s". Valid '.
81 'options are: %s.',
82 $new,
83 $key,
84 implode(', ', array_keys($options))),
85 $xaction);
86 continue;
90 return $errors;
93 public function getTransactionTypeForConduit($xaction) {
94 return 'behavior';
97 public function getFieldValuesForConduit($xaction, $data) {
98 return array(
99 'key' => $this->getBehaviorKeyForTransaction($xaction),
100 'old' => $xaction->getOldValue(),
101 'new' => $xaction->getNewValue(),
105 private function getBehaviorKeyForTransaction(
106 PhabricatorApplicationTransaction $xaction) {
107 $metadata_key = HarbormasterBuildPlanBehavior::getTransactionMetadataKey();
108 return $xaction->getMetadataValue($metadata_key);
111 private function getBehaviorKey() {
112 $metadata_key = HarbormasterBuildPlanBehavior::getTransactionMetadataKey();
113 return $this->getMetadataValue($metadata_key);
116 private function getBehavior() {
117 $behavior_key = $this->getBehaviorKey();
118 $behaviors = HarbormasterBuildPlanBehavior::newPlanBehaviors();
119 return idx($behaviors, $behavior_key);
122 private function getStorageKey() {
123 return HarbormasterBuildPlanBehavior::getStorageKeyForBehaviorKey(
124 $this->getBehaviorKey());