dimanche 28 août 2022

Different return values for use case

I have a structural problem in my PHP code which I want to get rid of, but I don't know how I can solve this in a clean matter (using a design pattern or whatever).

Imagine you have the following classes:

abstract class Person
{
  // return 0 = create, 1 = update, -1 error
  public function createOrUpdatePerson(string $firstname, string $lastname): int;
}

class DBPerson extends Person
{
  // overwrite
  public function createOrUpdatePerson(string $firstname, string $lastname): int;
}

class SessionPerson extends Person
{
  // overwrite
  public function createOrUpdatePerson(string $firstname, string $lastname): int;
}

Now, this functions are old and used by a frontend, which you don't want to touch. It expects you deliver an integer back. Now, you want to reuse this function in an API - but in this use case you want to return the id of the person instead of the operation code.

How would you handle that? I don't want to add an additional parameter (like isAPI or something like that) to respond with a different return type. For now, in case I'm in the API context, I call createOrUpdatePerson and afterwards try to fetch the item separately.

Aucun commentaire:

Enregistrer un commentaire