0
0
00O2021-08-04 12:01:23
PHP
00O, 2021-08-04 12:01:23

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);
}

and 2 classes that extend it with a bunch of different methods (each with their own), which call $this->send(...) inside themselves with the necessary parameters. The number of these methods is constantly increasing. APIs are being added. In controllers, we implement this or that API class not through abstraction, but by specifying a specific implementation. Yes, and they cannot have a common interface by definition, these are different APIs. It turns out that the abstract class is used only for sharing the send() method, and class methods for calling it with the necessary parameters.

I read https://habr.com/ru/post/313796/ , but I can't figure out how to implement the existing functionality, taking into account the principles from the article. If not difficult, please explain.

I can imagine if we need to replace, for example, storage. We change the MySql database to a file, all methods remain the same (i.e., the interface is the same), but the implementation changes. And here? Maybe I want to apply the principle to the wrong task?

UPD: I've been thinking. Replacement of implementation is not necessary to me, it turns out. I know exactly where in the code to access one API and where to another. And there you need exactly the implementation that is necessary in that place, without the possibility of replacing it. But this fact does not make the code better.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
profesor08, 2021-08-04
@profesor08

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.

V
Vitsliputsli, 2021-08-04
@Vitsliputsli

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 question

Ask a Question

731 491 924 answers to any question