<?php



/**
 * Skeleton subclass for performing query and update operations on the 'file' table.
 *
 * 
 *
 * This class was autogenerated by Propel 1.5.6 on:
 *
 * Mon Oct 24 09:36:19 2011
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package    propel.generator.lib.model
 */
class FilePeer extends BaseFilePeer {

  const ADDED    = 'A';
  const MODIFIED = 'M';
  const DELETED  = 'D';

  /**
   * @static
   * @param GitCommand $gitCommand
   * @param Branch $branch
   * @param string $lastBranchSynchronizationCommit
   * @return int 0 if succeed
   */
  public static function synchronize(GitCommand $gitCommand, Branch $branch, $lastBranchSynchronizationCommit = null)
  {
    $filesGit = $gitCommand->getDiffFilesFromBranch($branch->getRepository()->getGitDir(), $branch->getCommitReference(), $branch->getLastCommit());

    if(count($filesGit) > sfConfig::get('app_max_number_of_files_to_review', 4096))
    {
      return count($filesGit);
    }

    $filesModel = FileQuery::create()
      ->filterByBranchId($branch->getId())
      ->find()
    ;

    if(count($filesModel) > 0)
    {
      $diffFilesFromLastSynch = $gitCommand->getDiffFilesFromBranch($branch->getRepository()->getGitDir(), (!is_null($lastBranchSynchronizationCommit)) ? $lastBranchSynchronizationCommit : $branch->getCommitReference(), $branch->getLastCommit(), false);
    }

    foreach ($filesModel as $fileModel)
    {
      /** @var $fileModel File */
      if(!array_key_exists($fileModel->getFilename(), $filesGit))
      {
        $fileModel->delete();
      }
      else
      {
        $lastChangeCommit = $gitCommand->getLastModificationCommit($branch->getRepository()->getGitDir(), $branch->getName(), $fileModel->getFilename());

        if(isset($diffFilesFromLastSynch[$fileModel->getFilename()]))
        {
          $fileModel->setReviewRequest(true)
            ->setStatus(BranchPeer::A_TRAITER)
            ->setCommitStatusChanged($lastChangeCommit)
          ;
        }
        else
        {
          $fileModel->setReviewRequest(false);
        }

        if($filesGit[$fileModel->getFilename()]['is-binary'])
        {
          $fileModel->setIsBinary(true)
            ->setNbAddedLines(0)
            ->setNbDeletedLines(0);
        }
        else
        {
          $fileModel->setIsBinary(false)
            ->setNbAddedLines($filesGit[$fileModel->getFilename()]['added-lines'])
            ->setNbDeletedLines($filesGit[$fileModel->getFilename()]['deleted-lines']);
        }

        $fileModel->setState($filesGit[$fileModel->getFilename()]['state'])
          ->setLastChangeCommit($lastChangeCommit)
          ->setCommitInfos($gitCommand->getCommitInfos($branch->getRepository()->getGitDir(), $lastChangeCommit, "%ce %s"))
          ->setCommitReference($branch->getCommitReference())
          ->save();
        ;
      }

      unset($filesGit[$fileModel->getFilename()]);
    }

    foreach ($filesGit as $fileGit)
    {
      $lastChangeCommit = $gitCommand->getLastModificationCommit($branch->getRepository()->getGitDir(), $branch->getName(), $fileGit['filename']);
      $file = new File();

      if($fileGit['is-binary'])
      {
        $file->setIsBinary(true)
          ->setNbAddedLines(0)
          ->setNbDeletedLines(0);
      }
      else
      {
        $file->setIsBinary(false)
          ->setNbAddedLines($fileGit['added-lines'])
          ->setNbDeletedLines($fileGit['deleted-lines']);
      }

      $file->setFilename($fileGit['filename'])
        ->setStatus(BranchPeer::A_TRAITER)
        ->setState($fileGit['state'])
        ->setBranchId($branch->getId())
        ->setLastChangeCommit($lastChangeCommit)
        ->setCommitInfos($gitCommand->getCommitInfos($branch->getRepository()->getGitDir(), $lastChangeCommit, "%ce %s"))
        ->setCommitReference($branch->getCommitReference())
        ->setReviewRequest(true)
        ->save()
      ;
    }

    return 0;
  }
} // FilePeer
