Nasıl bir parametre benim sınıfta birkaç kez tekrarlar çarpanlarına mı?

1 Cevap php

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 Cevap

Bu "bir nesne özniteliği çevirmek" demek için cazip, ama bu sınıfın amacı veritabanını ele çünkü burada uygunsuz olurdu. Muhtemelen getVersions() sürüm kimliği saklanması ve yerine çeşitli versiyon-ID ilgili yöntemler olurdu nesnenin bazı diğer tip bir dizi dönmek için daha iyi olurdu.