2021年3月29日星期一

Is it possible to refactor this Controller create method in Laravel?

Is there a design pattern or good practices for this kind of view inizialization code? In this case, depending on the parameter ($tipoPedido) it sets up the view in different ways:

 public function create(Request $request, RepositorioDepositos $repositorioDepositos, RepositorioEmpresas $repositorioEmpresas)      {          $tipoPedido = strtoupper($request->tipo);            $datos = ['action' => "/api/pedidos", 'tipo' => $tipoPedido];              if ($tipoPedido == TipoRemitoStock::ESPECIAL) {                if (!tengoPermiso(\App\Permiso::STOCK_CREAR_REMITO_ESPECIAL)) {                  return mensajeErrorNoTienePermiso();              }                $datos['origen']  = null;              $datos['destino'] = $repositorioDepositos->getTodosNoFinalizados();          }                              elseif ($tipoPedido == TipoRemitoStock::INTERNO) {              $datos['origen']  = $this->getMisPosiblesDepositosOrigen($repositorioDepositos);              $datos['destino'] = $this->getMisPosiblesDepositosDestino($repositorioDepositos);          }                              elseif ($tipoPedido == TipoRemitoStock::PROVEEDOR) {              $datos['origen']      = null;              $datos['destino']     = $this->getMisPosiblesDepositosDestino($repositorioDepositos);              $datos["proveedores"] = $this->proveedoresConOCsDeMaterialesAprobadas($repositorioEmpresas);          }                    else {              die("Tipo de pedido no implementado: " . $tipoPedido);          }              $vista = view('ventas.formulario-abm-pedido', $datos);            return $vista;      }  

Now we need to add 3 new types so there will be 3 new elseif blocks.

https://stackoverflow.com/questions/66860790/is-it-possible-to-refactor-this-controller-create-method-in-laravel March 30, 2021 at 03:55AM

没有评论:

发表评论