How to fix single responsibility with an event listener in Laravel

Viewed 40

I have a model called product, which when updated emits an event inside the controller

public function update(Request $request, Product $product)
{
    $product = DB::transaction(function () use ($request,$product) {
        $product->update(...);
        return $product;
    });
    ProductUpdated::dispatch( $product);
}

Inside the listener that listens to this event, there is a model update in the sync_id field, it refers to an id that returns the call to an external api to be saved in the model.

Listener

public function handle(ProductUpdated $event)
{
    $sync_id = function_external_api();
    $event->product->sync_id= $sync_id;
    $event->product->save();
}

Doing this in the listener would break the S of solid (single responsability) , since it would be calling the api and updating the model. What alternative would there be for this scenario?

0 Answers
Related