mardi 23 janvier 2018

Laravel 5: How refactor a controller

I have this controller with some very similar methods. How can I refactor them? Thanks.

In each method, in addition to some basic variables, I collect the news from the database according to the section to which they belong (except in the first one, which includes all the sections), then I identify the language in order to make a manual pagination of news according to the language and finally I create the paginated in each language.

    class NoticiaController extends Controller
{
    public function actualidad(Request $request)
    {
        (isset($request->num))? $num = $request->num : $num = '6';
        $now = date('Y-m-d');
        $time = date('H:i:s');

        $data['images'] = Image::where('imageable_type', 'App\Models\Noticia')->get();
        $data['sections']  = Section::all();
        $data['ruta'] = 'actualidad';


            $data['noticias'] = Noticia::where('date', '<', $now)
                ->where('active', '1')
                ->orWhere('date', '=', $now)
                ->where('time', '<=', $time)
                ->where('active', '1')
                ->orderBy('date', 'desc')
                ->orderBy('time', 'desc')
                ->get();

        foreach($data['noticias'] as $row){
            foreach($row->langs as $row_lang) {
                if ($row_lang->lang_id == '1') {
                    $data['noticias-es'][] = $row;
                } elseif ($row_lang->lang_id == '2') {
                    $data['noticias-en'][] = $row;
                } elseif ($row_lang->lang_id == '3') {
                    $data['noticias-pt'][] = $row;
                } else {
                }
            }
        }

        // Paginado manual
        /*  Get current page form url e.x. &page=1
            Create a new Laravel collection from the array data
            Slice the collection to get the items to display in current page
            Create our paginator and pass it to the view
            set url path for generted links
        */
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        // español
        $itemCollection = collect($data['noticias-es']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-es'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-es']->setPath($request->url());

        // inglés
        $itemCollection = collect($data['noticias-en']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-en'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-en']->setPath($request->url());

        // portugués
        $itemCollection = collect($data['noticias-pt']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-pt'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-pt']->setPath($request->url());

        $data['title'] = __('header.actualidad');

        return view('web.actualidad.listado', compact('data'));
    }

    public function noticias(Request $request)
    {
        (isset($request->num))? $num = $request->num : $num = '6';
        $now = date('Y-m-d');
        $time = date('H:i:s');

        $data['noticias'] = Noticia::where('date', '<', $now)
            ->where('active', '1')
            ->where('section_id', '1')
            ->orWhere('date', '=', $now)
            ->where('time', '<=', $time)
            ->where('active', '1')
            ->where('section_id', '1')
            ->orderBy('date', 'desc')
            ->orderBy('time', 'desc')
            ->get();

        foreach($data['noticias'] as $row){
            foreach($row->langs as $row_lang) {
                if ($row_lang->lang_id == '1') {
                    $data['noticias-es'][] = $row;
                } elseif ($row_lang->lang_id == '2') {
                    $data['noticias-en'][] = $row;
                } elseif ($row_lang->lang_id == '3') {
                    $data['noticias-pt'][] = $row;
                } else {
                }
            }
        }

        // Paginado manual
        /*  Get current page form url e.x. &page=1
            Create a new Laravel collection from the array data
            Slice the collection to get the items to display in current page
            Create our paginator and pass it to the view
            set url path for generted links
        */
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        // español
        $itemCollection = collect($data['noticias-es']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-es'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-es']->setPath($request->url());

        // inglés
        $itemCollection = collect($data['noticias-en']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-en'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-en']->setPath($request->url());

        // portugués
        $itemCollection = collect($data['noticias-pt']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-pt'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-pt']->setPath($request->url());

        $data['images'] = Image::where('imageable_type', 'App\Models\Noticia')->get();
        $data['sections']  = Section::all();
        $data['section_id'] = '1';
        $data['ruta'] = 'noticias';

        $data['title'] = __('header.noticias');

        return view('web.actualidad.listado', compact('data'));
    }

    public function testimonios(Request $request)
    {
        (isset($request->num))? $num = $request->num : $num = '6';
        $now = date('Y-m-d');
        $time = date('H:i:s');

        $data['noticias'] = Noticia::where('date', '<', $now)
            ->where('active', '1')
            ->where('section_id', '3')
            ->orWhere('date', '=', $now)
            ->where('time', '<=', $time)
            ->where('active', '1')
            ->where('section_id', '3')
            ->orderBy('date', 'desc')
            ->orderBy('time', 'desc')
            ->get();

        foreach($data['noticias'] as $row){
            foreach($row->langs as $row_lang) {
                if ($row_lang->lang_id == '1') {
                    $data['noticias-es'][] = $row;
                } elseif ($row_lang->lang_id == '2') {
                    $data['noticias-en'][] = $row;
                } elseif ($row_lang->lang_id == '3') {
                    $data['noticias-pt'][] = $row;
                } else {
                }
            }
        }

        // Paginado manual
        /*  Get current page form url e.x. &page=1
            Create a new Laravel collection from the array data
            Slice the collection to get the items to display in current page
            Create our paginator and pass it to the view
            set url path for generted links
        */
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        // español
        $itemCollection = collect($data['noticias-es']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-es'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-es']->setPath($request->url());

        // inglés
        $itemCollection = collect($data['noticias-en']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-en'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-en']->setPath($request->url());

        // portugués
        $itemCollection = collect($data['noticias-pt']);
        $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
        $data['noticias-pt'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
        $data['noticias-pt']->setPath($request->url());

        $data['images'] = Image::where('imageable_type', 'App\Models\Noticia')->get();
        $data['sections']  = Section::all();
        $data['section_id'] = '3';
        $data['ruta'] = 'testimonios';

        $data['title'] = __('header.testimonios');

        return view('web.actualidad.listado', compact('data'));
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire