php - Same parameter being passed to many methods, how can I refactor it? -


i used repository pattern because updating database schema @ point in future. current schema badly designed, many attributes constitute single unidade entity spread out in 3 tables, each table created unidade 1 row, switched rows tables) , fourth has row each unidade entity (follows correct convention).

due situation recommended use repository interface, integrate new database schema in future while unifying 4 tables in single database interface.

this have:

public function createobject($un)  {     $produto       = $this->configrepository->getproduto($un);     $ultimonivel   = $this->configrepository->getdataultimonivel($un);     $nivelestimado = $this->configrepository->getultimonivel($un);     $ultimamedia   = $this->configrepository->getdataultimamedia($un);     $mediainterna  = $this->configrepository->getultimamedia($un);     $nrcabos       = $this->configrepository->getqtdcabostotal($un);     $nivellivre    = $this->configrepository->getnivelestimadolivre($un);      $ncaboslaterais = $this->configrepository->getqtdcabolateral($un);     $nsensorescaboslaterais = $this->configrepository->getqtdsensorcabolateral($un);     $nsensorescabocentral = $this->configrepository->getqtdsensorcabocentral($un);               // i'm creating generic object :/     $unidade = (object) compact(         'produto',          'ultimonivel',          'nivelestimado',          'ultimamedia',          'mediainterna',          'nrcabos',          'nivellivre',         'arquivocss'         );      return $unidade; } 

sorry variables in portuguese, configrepository methods query database configuration option (it attribute) , return it, number of cables, space occupied, dates , basic information. each option attribute of unidade entity.

i thinking of refactoring work this:

$this->configrepository->setunidadeid($un);  $produto       = $this->configrepository->getproduto(); $ultimonivel   = $this->configrepository->getdataultimonivel(); // ..... other methods 

but have other thing doesn't pretty:

    // i'm creating generic object :/     $unidade = (object) compact(         'produto',          'ultimonivel',          'nivelestimado',          'ultimamedia',          'mediainterna',          'nrcabos',          'nivellivre'              ); 

all information describe 1 object unidade.

ideally, think more correct create new class called "unidade" class, dependency inject repository , initialize attributes reference options class attributes:

class unidade{     public function __construct($un, configrepositoryinterface $configrepo)     {         $this->configrepository = $configrepo->setunidadeid($un);         $this->produto = $this->configrepository->getproduto();         $this->ultimonivel = $this->configrepository->getultimonivel();         //etc ...     } } 

and use in code this:

$unidadeobj = new unidade($un); 

whats best way refactor it? i'm using laravel 4.2.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -