mercredi 28 novembre 2018

Laravel refactoring - How can my methods be refactored?

I'm still at a beginner level in Laravel - so refactoring is something I need to sort out.

So I have 2 methods which are bringing in API's, one shows upcoming football fixtures, and one brings in results. Both share the same auth data under the $fixtures and $results variable. The $url is pretty much the same except the fixtures and results keyword part.

I am not entirely sure how I can refactor this. Do I need to create an extra function with the duplicated data and then call it in my other 2 methods? I'm not 100% sure how to write this!

protected function getFixtures()
{
    $url = 'https://sportdata.p.mashape.com/api/v1/free/soccer/matches/fixtures/premier-league';

    $fixtures = Zttp::withHeaders([
        "X-Mashape-Key" => "WxLtGy9Mx6msheZOC3IISAGlqUcDp1qkbudjsnpL91tbHWQTPF",
        "Accept" => "application/json"
    ])->get($url, []);

    return $fixtures->json();
}

protected function getResults()
{
    $url = 'https://sportdata.p.mashape.com/api/v1/free/soccer/matches/results/premier-league';

    $results = Zttp::withHeaders([
        "X-Mashape-Key" => "WxLtGy9Mx6msheZOC3IISAGlqUcDp1qkbudjsnpL91tbHWQTPF",
        "Accept" => "application/json"
    ])->get($url, []);

    return $results->json();
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire