<?php

final class DifferentialSetBaseCommitConduitAPIMethod
  extends DifferentialConduitAPIMethod {

  public function getAPIMethodName() {
    return 'differential.hack.setbasecommit';
  }

  public function getMethodDescription() {
    return pht('(Temporary Hack) Set the base commit of a diff.');
  }

  protected function defineParamTypes() {
    return array(
      'diffID' => 'required id',
      'commit' => 'required string',
    );
  }

  protected function defineReturnType() {
    return 'null';
  }

  protected function execute(ConduitAPIRequest $request) {
    $viewer = $request->getUser();

    $diff_id = $request->getValue('diffID');
    if (!strlen($diff_id)) {
      throw new Exception(
        pht(
          'To select a diff to affect, provide a "diffID".'));
    }

    $diff = id(new DifferentialDiffQuery())
      ->setViewer($viewer)
      ->withIDs(array($diff_id))
      ->executeOne();
    if (!$diff) {
      throw new Exception(
        pht(
          'No diff exists with ID "%s".',
          $diff_id));
    }

    $commit = $request->getValue('commit');
    if (!strlen($commit)) {
      throw new Exception(
        pht(
          'Provide a "commit" to identify which commit to set as '.
          'the base.'));
    }

    $diff
      ->setSourceControlBaseRevision($commit)
      ->save();

    return null;
  }

}