dimanche 29 novembre 2015

SOLID Principle In Laravel with Repository Pattern

I have some confusion about use of Controller with Repository Pattern while maintaining SOLID Principle. Consider, I have two types of Quotations

  1. Commercial Quotation
  2. Private Quotation

And there is a high chance of new types of quotations in future. Each Quotations has different fields, business logics yet they share many common functions. So I created a QuotationInterface

Quotation Inteface

interface QuotationInterface
{   
    public function save(array $data);

}

Quotation class that implement the interface

class CommercialQuotation implements QuotationInterface
{   
    public function(array $data)
    {
        // save commercial quotation
    }
}

class PrivateQuotation implements QuotationInterface
{   
    public function(array $data)
    {
    // save Private quotation
    }
}

Quotation Repository

class QuotationRepository 
{
    public function save(array $data, QuotationInterface $quotation)
    {
        $quotation->save($data);
    }
}

QotationController

public function store(Resource $resource)
{

    $inputs = $resource->all();

    /**
    *  Clearly here Open/Close Principle is broken
    */

    if ($inputs['type'] == 'private'){

           $quotation = new PrivateQuotation;;

    }else if($inputs['type'] == 'commercial'){

           $quotation = new CommercialQuotation;

    }

    $this->repo->save($inputs, $quotation);
}

Here in my QuotationController, it is clearly violating Open/Close Principle..

Is it a good idea to Create a Controller for each type of quotation (might be 10+ some day, who know?) to avoid the OCP violation or my design is just wrong? Any suggestion, design change tips, resource are welcome.

NOTE: My Quotation Controller will have many other functions except the save only.

Aucun commentaire:

Enregistrer un commentaire