T
T
Timur Asgard2017-10-14 00:35:09
Laravel
Timur Asgard, 2017-10-14 00:35:09

What code should be moved to separate classes?

Hey!
I can't figure out at what point repetitive code should be moved to separate classes (let's call them handler(s)) from controllers.

  • Suppose the controller has the following code $result = ['my_data' => $var]; and somewhere in another controller the line is repeated. And now, because of this, you need to come up with the name of the handler (a) characterizing this process and transfer the code there?
  • And if the controller performs 5 large actions at the same time? Do I have to transfer all this into 1 handler (a)? It will turn out a God class (this is not good), but I can’t separate them either, since the code is very closely related.

In general, put on the right path.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergeyj, 2017-10-14
@sayber

You don't have to call them handlers.
Taking the example $result = ['my_data' => $var];
That is not necessary of course. But I understand what you mean. Although if you operate with objects to the fullest, then you can create something like responseMyData
The controller receives the request and gives the response by calling the handler. There shouldn't be a line of code in it anymore.
With the exception of some individual implementations. Here is an example of my controllers. removed under

Controller code - spoiler
<?php

class orderController extends AbstractController {

    /**
     * @ApiDoc(
     *     section="travelSystem",
     *     input=OrderRequest::class,
     *     statusCodes={
     *          200="sucess",
     *          404="Order not found"
     *     },
     *     views={"night_build"}
     * )
     * @RestDoc(
     *     security={Access::ORDER}
     * )
     * 
     * @return Response
     */
    public function orderAction(OrderRequest $orderRequest): Response
    {
        // Команда - простой объект с геттерами
        $command = new orderCommand(
            $orderRequest->getHotel(),
            $orderRequest->getCheckIn()
        );
        // Хендлер - в котором как раз и происходят все 
        // нужные нам операции по обработке
        $this->get('commandBus')->handle($command);

        return $this->responseForRest();
    }
}

As you can see, there are some peculiarities here, but the controller is thin.
What happens in the current handler?
The existence of duplicate entries is checked there (and this is a turnip call and a condition), then the model for order is called, which receives commands and goes to write. This has to be duplicated, because the code is essentially individual for each handler, but similar in functionality.
But after writing to the database, a listener is called, which causes an event to write logs.
This operation is needed everywhere and therefore it is placed in a separate object that can be called in any handler, by means of a DI constructor
Further, it's even worse =) The logs have their own handler and their own chain of events.
Let's say we write something to the blog, send our command to the handler, or whatever may be in laravel where processing takes place. In processing there can be a record in a DB and also loading of the image.
Here is the image upload, it is worth highlighting it as a separate service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question