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();
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();
35 $behavior_name = $this->getBehaviorKey();
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) {
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(
58 'No behavior with key "%s" exists. Valid keys are: %s.',
60 implode(', ', array_keys($behaviors))),
65 $behavior = $behaviors[$key];
66 $options = $behavior->getOptions();
68 $storage_key = HarbormasterBuildPlanBehavior
::getStorageKeyForBehaviorKey(
70 $old = $object->getPlanProperty($storage_key);
71 $new = $xaction->getNewValue();
77 if (!isset($options[$new])) {
78 $errors[] = $this->newInvalidError(
80 'Value "%s" is not a valid option for behavior "%s". Valid '.
84 implode(', ', array_keys($options))),
93 public function getTransactionTypeForConduit($xaction) {
97 public function getFieldValuesForConduit($xaction, $data) {
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());