samedi 5 décembre 2015

API Calls in Laravel Repository Model

I am developing a web application in which I will be using thrid party API integrations (Payment gateways, SMS Vendors, Emailing services like mailchimp, other external APIs). My application will be having the repository pattern which will have repositories associated with each of my models and my controllers will be utilizing the repository functions.

Now where does all my API integrations comes in? Should I be keeping all my API integrations in a single repository?

Example :

If I have a SMS provider Valuefirst, how does my laravel repository design look like?

My SMS interface

interface ISmsProvider {
    public function sendSms($mobileNo,$msg);
}

My ValueFirst (SMS Provider) repository

class ValueFirstRepository implements ISmsProvider {
    public function sendSMS($mobieNo,$msg)
    {
       //API Call to ValueFirst SMS provider
       // Any database insertions/updates to SMS model
       // Any file logs
    }
}

Assuming I have a service provider class where I have done the binding of the interface and repository my controller will look like

SMS Controller

class SmsController extends BaseController {
    // ISmsProvider is the interface
    public function __construct(ISmsProvider $sms)
    {
        $this->sms = $sms;
    }

    public function sendSMS()
    {
        $mobileNo = '9999999999';
        $msg = 'Hello World!';
        $users = $this->sms->sendSMS($mobileNo,$msg); 
    }    
}

My questions

  1. Is this the right approach to an API integration using repository pattern?
  2. Does anything related to any database activities happens in the ValueFirst repository? Eg : If I want to update a record after the API response from ValueFirst, do I do it in the respository?
  3. If its right , can someone show me how the model interactions happens from the respository?
  4. Now if I need to choose between ValueFirst or any other vendor how does that happens? How can I do it from the controller?

I will be using this project as a skelton for my application

l5-Respository

Please help me with this design, I am fairly new to laravel / repository pattern. Trying to learn stuff. TIA :)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1IvsH10
via IFTTT

Aucun commentaire:

Enregistrer un commentaire