Answer the question
In order to leave comments, you need to log in
How to implement the functionality, taking into account the principle of dependency inversion?
There is a Laravel project that accesses several internal REST APIs.
Now it is implemented like this:
There is an abstract class that has a method
protected function send(string $verb, string $endpoint, array $params = []): Response
{
return $this->http->$verb($endpoint, $params);
}
Answer the question
In order to leave comments, you need to log in
Different APIs for different tasks, respectively, they will have different classes and methods. In common, they will only have a mechanism for sending requests, which can be added by inheritance, or extended through traits.
What other classes of different APIs? REST API is an interaction mechanism, it is an internal implementation, which for some reason is pulled to the top level, to the level of controllers. You must have business entities, and apply the Principles to the objects of those entities. And already at a lower level, to transfer data of business entities, use an object of the RestAPI class, in which, of course, there should not be any bindings on the specifics of higher-ranking business entities.
Your send method looks weird, why is it needed at all? Dynamic methods are very inconvenient, the IDE will not tell you anything about them and you will have to solve this puzzle yourself, but this method does nothing more.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question