IMPORTANT: Per accedir als fitxer de subversion: http://acacha.org/svn (sense password). Poc a poc s'aniran migrant els enllaços. Encara però funciona el subversion de la farga però no se sap fins quan... (usuari: prova i la paraula de pas 123456)

See Single Responsibility Principle example with Laravel article


Solution could be found at tag solution_case_s of github repo acacha/solid_laravel:

https://github.com/acacha/solid_laravel/tree/solution_case_s

InvoiceReport class:

<?php namespace SolidLaravel;

use SolidLaravel\Output\Contracts\InvoiceShowInterface;
use SolidLaravel\Repositories\InvoiceRepository;

class InvoiceReport {


    private $invoice;

    function __construct(InvoiceRepository $repo, $id)
    {
        $this->invoice = $repo->get($id);
    }

    public function show(InvoiceShow $i){
        return $i->show($this->invoice);
    }
}

Where Authentication is controlled using Middleware at controller:

<?php namespace SolidLaravel\Http\Controllers;

use SolidLaravel\Http\Requests;
use SolidLaravel\Http\Controllers\Controller;

use Illuminate\Http\Request;
use SolidLaravel\InvoiceReport;
use SolidLaravel\Output\InvoiceShowHtml;
use SolidLaravel\Repositories\InvoiceRepository;

class InvoiceController extends Controller {

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show invoice
     *
     * @return Response
     */
    public function index()
    {
        $invoice = new InvoiceReport(new InvoiceRepository(),1);
        return view('invoice')->with('totalAmmount',$invoice->show(new InvoiceShowHtml()));
    }

}

Persistence now use Independency injection and repository pattern. At App/Repositories folder I added InvoiceRepository class:

<?php
/**
 * Created by Sergi Tur Badenas @2015
 * http://acacha.org/sergitur
 * http://acacha.org
 * Date: 08/04/15
 * Time: 18:17
 */

namespace SolidLaravel\Repositories;

use SolidLaravel\Invoices;

class InvoiceRepository {

    /**
     * Return invoice from the database
     * @param $id - The ID of the requested invoice
     * @return Eloquent object
     */
    public function get($id) {
        return Invoices::find($id);
    }

}

Create a InvoiceShow class:

<?php
/**
 * Created by Sergi Tur Badenas @2015
 * http://acacha.org/sergitur
 * http://acacha.org
 * Date: 08/04/15
 * Time: 18:24
 */

namespace SolidLaravel\Output;

class InvoiceShow{

    public function show($invoice)
    {
        return "<strong>" . $invoice->totalAmmount . " </strong>";
    }
}