<?php

final class ExtensionStoreExtensionPublisherTransaction
  extends ExtensionStoreExtensionTransactionType {

  const TRANSACTIONTYPE = 'est:extension:publisher';

  public function generateOldValue($object) {
    return $object->getPublisherPHID();
  }

  public function applyInternalEffects($object, $value) {
    $object->setPublisherPHID($value);
  }


  public function validateTransactions($object, array $xactions) {
    $errors = array();

    if (!$xactions) {
      if (!$object->getPublisherPHID()) {
        $errors[] = $this->newInvalidError(pht('PublisherPHID is required!'));
      }
    }


    foreach ($xactions as $xaction) {
      $new_value = $xaction->getNewValue();
      // TODO figure out why getOldValue is null
      $old_value = $xaction->getOldValue();
      $old_value = $object->getPublisherPHID();

    if ($new_value === null) {
      $errors[] = $this->newInvalidError(pht("can't be mpty"));
    }
      if ($old_value !== null && $new_value != $old_value) {
        $errors[] = $this->newInvalidError(
          pht('PublisherPHID cannot be changed.'));
      }
    }

    return $errors;
  }
}
