views:

34

answers:

1

Hello,

I show you firsly my class : as you can see I repeated several times $versionId param because all the method needs it. I am asking myself if there is a way to factorize it so there is lesser repetition.

<?php
class Admin_Model_Version
{
  private $_db;
  private $_versionId;
  private $_path;

  public function __construct()
  {

  }

  /**
  * Récupère l'objet de la table version.
  *
  * @return Zend_Db_Table
  */
  public function getDb()
  {
    if(!isset($this->_db))
    {
      $this->_db = new Admin_Model_DbTable_Version();
    }

    return $this->_db;
  }

  /**
  * Retourne toutes les versions d'un projet.
  *
  * @param integer $versionId
  */
  public function getVersions($projectId)
  {
    $db     = $this->getDb();
    $select = $db->select();

    return $select
    ->where("project_idproject = ?", $projectId)
    ->query()->fetchAll();
  }

  public function getVersionPath($versionId)
  {
    $root          = realpath(dirname($_SERVER['SCRIPT_FILENAME']) . "/../");
    $patrimonyName = $this->getPatrimonyRecordByVersionId($versionId)->name_patrimony;
    $projectName   = $this->getProjectRecordByVersionId($versionId)->name_project;
    $versionName   = $this->getVersionRecordByVersionId($versionId)->lab_version;

    return $root . "/data/projects/" . $patrimonyName . "/" . $projectName . "/" . $versionName . "/";
  }

  /**
  * Vérifie si la version possède un repertoire de travail.
  *
  * Tous les répertoires de travail sont dans le répertoire /data/projects/
  */
  public function hasVersionDirectory($versionId)
  {
    $versionPath = $this->getVersionPath($versionId);

    // Si le chemin n'existe pas retourner une exception.
    if(!realpath($versionPath))
    {
      throw new Exception("<b>Admin_Model_Version</b> " . __LINE__ . " : Le répertoire n'existe pas!");

      return false;
    }
    else
    {
      return true;
    }
  }

  public function getPatrimonyRecordByVersionId($versionId)
  {
    $db = $this->getDb();

    // setIntegrityCheck(false) is required for join
    $row = $db->select()->setIntegrityCheck(false);

    return $row->from(array('ve' => 'version'), array())
    ->join(array('pr' => 'project'), 've.project_idproject = pr.idproject', array())
    ->join(array('pa' => 'patrimony'), 'pa.idpatrimony = pr.patrimony_idpatrimony')
    ->where('ve.idversion = ?', $versionId)
    ->query()->fetchObject();
  }

  public function getProjectRecordByVersionId($versionId)
  {
    $db = $this->getDb();

    // setIntegrityCheck(false) is required for join
    $row = $db->select()->setIntegrityCheck(false);

    return $row->from(array('ve' => 'version'), array())
    ->join(array('pr' => 'project'), 've.project_idproject = pr.idproject')
    ->where('ve.idversion = ?', $versionId)
    ->query()->fetchObject();
  }

  public function getVersionRecordByVersionId($versionId)
  {
    $db = $this->getDb();

    // setIntegrityCheck(false) is required for join
    $row = $db->select();

    $row->where('idversion = ?', $versionId);

    return $row->query()->fetchObject();
  }

  /**
  * Crée le répertoire de travail avec comme chemin le nom du patrimoine
  * suivi du nom de projet et du nom de version.
  */
  public function createHome($versionId)
  {
    $path = $this->getVersionPath($versionId);
    Zend_Registry::get('firephp')->info($path);

    if(!mkdir($path, 0755, true))
    {
      throw new Exception(__METHOD__ . "can't create directory");
    }
  }

  public function hasDirectorySRC($versionId)
  {
    $path =
  }

  public function hasDirectoryHTML($versionId)
  {

  }

  public function hasDirectoryXML($versionId)
  {

  }

  public function hasDirectorySVG($versionId)
  {

  }
}

Thx

+1  A: 

It's tempting to say "turn it into an object attribute", but that would be inappropriate here since the purpose of this class is to handle the database. It would probably be better to have getVersions() return an array of some other type of object which would encapsulate the version ID and have the various version-ID-related methods instead.

Ignacio Vazquez-Abrams
I think your comment join up with the one of Ondrej. Thx too :-)
toddoon