The flow would be:
1 command that get's the data from ApiX,
it then loops over the dataset and for each block of data, it triggers another command (SendDataToApiY)
Each class should only have 1 responsibility:
1 class that communicates with API X
1 class that communicates with API Y
1 class that only transforms data from / to API X's standard to your internal one
1 class that only transforms data from / to API Y's standard to your internal one
and 1 class to bind this all, which is the command that you run to get the data from API X, transform it into the internally used data structure, and runs a command to send that data to Api Y.
OK, some quick pseudo code to get you up and running:
Create 2 classes for communicating with API X and Y
App\Apis\ApiX.php:
class ApiX
{
private $url = 'https://endpoint.to/api';
public function sendData($data)
{
// use cURL or something to actually send the data
// have some error checking here (throwing exceptions when things go wrong)
// return true on success
return true;
}
public function getData()
{
// use cURL or something to actually get the data
// throw Exceptions when things go wrong
// return $data on success
return $result;
}
}
App\Apis\ApiY.php:
class ApiY
{
private $url = 'https://another.endpoint.to/api';
public function sendData($data)
{
// use SOAP or something to actually send the data
// throw Exceptions when things go wrong
// return true on success
return true;
}
public function getData()
{
// use SOAP or something to actually get the data
// throw Exceptions when things go wrong
// return $data on success
return $data;
}
}
Create some adapter classes
App\Adapters\ApiXAdapter.php
App\Adapters\ApiYAdapter.php
class ApiXAdapter
{
public function transformFromArray(array $data)
{
// might be implemented, doesnt need to be if you only GET data from this API
}
public function transformToArray($data)
{
// $data would be the result you get from ApiX, let's assume it's json
$data = json_decode($data, true);
// dont just return the array as you got it form API X, make sure that you always know which structure your internally used dataset uses.
return [
'name' => $data['how-they-call-this-key'],
'address' => $data['how-they-call-this-key'],
'zipcode' => $data['how-they-call-this-key'],
];
}
}
class ApiYAdapter
{
public function transformFromArray(array $data)
{
// we know that the keys name, address and zipcode will exist
// but API Y wants SOAP XML instead of an array
// [ do some magic stuff to convert the array into XML]
return $xml;
}
public function transformToArray($data)
{
// might be implemented, doesnt need to be if you only SEND data to this API
}
}
Create a Job class: SendDataToApiY
class SendDataToApiY implements ShouldQueue
{
private $data;
use InteractsWithQueue, Queueable, SerializesModels;
public function __construct($data)
{
$this->data = $data;
}
public function handle()
{
$api = app()->make(ApiY::class);
$adapter = app()->make(ApiYAdapter::class);
Try {
// $this->data is the array as gotten from the ApiXAdapter
$data = $adapter->transformFromArray($this->data);
// $data now is the exact data you want to send to $apiY
$api->sendData($data);
} catch(\Exception $e) {
// something went wrong in the adapter or apiY, log this message
\Log::error('SendDataToApiY Error: '. $e->getMessage());
}
}
}
Create a command class: GetApiXData
class GetApiXData extends Command
{
public function handle()
{
$api = app()->make(ApiX::class);
$adapter = app()->make(ApiXAdapter::class);
$data = $api->getData();
foreach($data as $row) {
// $row is the data as gotten from API X
$row = $adapter->transformToArray($row);
// $row is now the data as transformed by the ApiXAdapter
dispatch(new SendDataToApiY($row));
}
}
}
In this way I'm not using any events or listeners, but just the queue system.
You can trigger the GetApiXData from an artisan command.
It will get the data from Api X, transform it into something usable
and for each block of data it got, it will dispatch another job onto the queue (SendDataToApiY)
Which will then transform the data back into something ApiY can handle, and send it on it's way.
I hope you get the gist of it.
If you want to learn about design patterns, I can highly recommend Php Objects, Patterns And Practice from Matt Zandstra.
(on google you can even find some PDF versions at some uni websites)