I have a Provider class and decorating one, CacheProviderDecorator class. The trouble is that I need to cache the result of method, which is called inside of other provider's method:
For example, in Provider class:
public function getUser(int $id): array
{
return json_decode($this->client->get('user/' . $id)->getBody()->getContents(), true);
}
public function getUsers(array $usersIds): array
{
$usersSurnames = new Collection();
foreach ($usersIds as $userId) {
$usersSurnames->push($this->getUser($userId)['surname']);
}
return $usersSurnames->toArray();
}
CacheProviderDecorator class:
public function getUser(int $id): array
{
$key = self::PROJECT_USER . '_' . $id;
return $this->cache->remember($key, self::TTL, function () use ($id) {
return $this->provider->getUser($id);
});
}
And because of getUser()
is only used inside of getUsers()
, I have to duplicate getUsers()
method from Provider, else the Provider's getUsers()
will be always called, and Provider's getUser()
then too (because of $this->).
There are a plenty of such repeats. Is there any good architectural solution instead?
Aucun commentaire:
Enregistrer un commentaire