PHP code example of mindwingx / service-call-adapter

1. Go to this page and download the library: Download mindwingx/service-call-adapter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mindwingx / service-call-adapter example snippets


php artisan sc:new <your-service-name>

use Mindwingx\ServiceCallAdapter\ServiceCall;

try {
    $response = ServiceCall::handle()->yourServiceName(["dummy-payload"]);
} catch (\Exception $exception) {
    return $exception->getMessage();
}

return $response;


class DummyServiceAdapter implements ServiceCallAdapterInterface
{
    /**
     * @var ServiceCallInterface
     */
    private ServiceCallInterface $service;

    public function __construct()
    {
        /*
         * Note: you make multiple Service Call Classes and handle them here to
         * access by the related condition, etc.
         */

        $this->service = new FirstServiceCall();
    }

    /**
     * @throws GuzzleException
     */
    public function call(array $payload = []): ResponseInterface|array
    {
        return $this->service
            ->preparePayload($payload)
            ->getResult();
    }
}

class FirstServiceCall extends ServiceCallHandler
{

    public function preparePayload(array $payload = []): self
    {
        //todo: set service call details
        $this->setUrl('https://service-url.io/api')
             ->setMethod() //  default: GET
             ->setQuery([])
             ->setHeaders([])
             ->setBody($payload);

        return $this;
    }

    /**
     * @throws GuzzleException
     */
    public function getResult(): ResponseInterface|array
    {
        return $this->sendRequest()
                    ->getArrayResponse();
    }
}