tmarois / laravel-ads-sdk

PHP SDK for Google Ads, Bing Ads, and Facebook Ads API for Laravel
MIT License
113 stars 46 forks source link

[1.4] Add macroable trait to service classes for easy extension #38

Closed modernben closed 3 years ago

modernben commented 3 years ago

This allows quick extension of the base Service classes using macros in your AppServiceProvider. Instead of having to wire it all up yourself in multiple places or submit a PR to get the method added, you can easily add your own methods to the Service classes.

This would also utilize the awesome auth that is already in place meaning you don't have to recreate all of that.

Usage example:

Service::macro('addUetTags', function($tags){
    $serviceCall = $this->call(ServiceClientType::CampaignManagementVersion13);

    try {
        $requestTags = [];
        foreach($tags as $tag) {
            $tag = new UetTag();
            foreach($tag as $key => $value) {
                $tag->{$key}  = $value;
            }

            array_push($requestTags,$tag);
        }

        $request = new AddUetTagsRequest();
        $request->UetTags = $requestTags;

        $serverResponse = $serviceCall->GetService()->AddUetTags($request);

        return $serverResponse;
    } catch (\Exception $e) {
        print $serviceCall->GetService()->__getLastRequest()."\n";
        print $serviceCall->GetService()->__getLastResponse()."\n";
    }
});
timothymarois commented 3 years ago

Thanks for this addition, I will push this into the master and prepare for a new v1.4!

If you want to add additional details in the readme so others can understand how to implement this that would be awesome.

Thanks again, and have a great weekend!

modernben commented 3 years ago

@timothymarois I'll add some verbiage and a code example. Feel free to edit if you like.

modernben commented 3 years ago

@timothymarois Added some documentation and updated the changelog

modernben commented 3 years ago

@timothymarois Just checking in on this. Let me know if you need anything else.

timothymarois commented 3 years ago

This is great! Thanks so much. Everything looks good so far. I'm going to push it this weekend.

modernben commented 3 years ago

Just checkin in on this again @timothymarois

timothymarois commented 3 years ago

@modernben This has been released! Let me know if you find any issues.