vendredi 12 février 2016

A good pattern to manage my controller api data calls

I've taken over a Symfony 2.6 project and I'm busy trying to refactor the fat controllers by as much as possible.

Currently the jist of all the controllers follow a similar structure

    /**
     * @Method({"GET"})
     * @Route("/", name="homepage")
     * @Template()
     */
    public function indexAction() 
    {
    //Initialize the API
    $api = $this->get('core_bundle.api_wrapper')->getApi();

    //Retrieve data from various end points
    $result_set = $api->get('/rest/resource');
    $result_set2 = $api->get('/rest/resource2');
    $result_set3 = $api->get('/rest/resource3');
    //etc.. etc..

//Once all api calls have been completed, do some manipulation of the data if required

    $result_set = DataHelper::getInstance()->setReadFlags($result_set);

    //The api resultset keys are not always standard (It's just the way it is Stackoverflow, I cannot change the API code)
    $result_set2 = Standardize::getInstance()->standardizeKeys($result_set2);
    $result_set2 = DataHelper::getInstance()->setReadFlags($result_set2);

    //Create the social links from the existing data
    $result_set3 = SocialHelper::getInstance()->createSocialLinks($result_set3);

return [$result_set, $result_set2, $result_set3];
}

I'm struggling to find a good pattern to apply to this type of scenario. I need to clean up the controllers and move as much business logic out of it because some of the controllers have 100+ lines of code.

The problem I have is:

  1. Each controller can make multiple calls to the API for data. The data is not always related and sometimes the calls are dependent on the Users Role and/or on a key from a previous API Call.
  2. Each result set will need to be manipulated in some way before being returned into the View Response.

I feel that creating multiple models then passing in the API Result set, then calling a function to manipulate each result set will result in a smaller controller action, but confusing to follow as I'll be calling multiple models multiple times per result set.

It will end up in having to create a lot of model classes if I want to keep inline with Single Responsibility.

Any guidance will be appreciated.

Aucun commentaire:

Enregistrer un commentaire