S
S
superkolya2019-02-15 09:44:29
Design patterns
superkolya, 2019-02-15 09:44:29

DRY in controller actions?

Let's say we have 2 controller actions that are very similar in content, the difference is only in the name of the view. How is it customary in mvc frameworks to take out common code? Are there such concepts like ControllerHelpers or similar? Or just make a Namespace like
HTTP -> Controllers
HTTP -> ControllerHelpers
And each helper class is just a class that contains methods that we then call in the controller?
For example this method:

public function ololoAction()
{
        $form_data = $this->request->getJsonRawBody(true);

        (new OloloValidation())->assert($form_data);

        $id_variable = 'policy_id';
        $token_variable = 'object_token';
        $per_page_variable = 'per_page';

        $query_string = (new JuridicalDmsStructures('alias', [$id_variable => 'Long!', $token_variable => 'String!', $per_page_variable => 'Long!']))
            ->addStructure($id_variable, $token_variable, false, $per_page_variable)
            ->build();

        $query = [
            'query' => $query_string,
            'variables' => [
                $id_variable => $policy_id,
                $token_variable => $object_token,
                $per_page_variable => 1
            ]
        ];

        $result = (new Request())->post('', soglasie_gql_request_options(), $query);

        $contract_data = Fractal::create()
            ->item($result['data']['data']['policy'],  OloloTransformer::class)
            ->serializeWith(WithoutDataKeySerializer::class)
            ->toArray();

        if(!isset($contract_data['manager']['work_email'])){
            throw new UserableException($this->t->_('mailer.failed_to_get_manager_email'));
        }

        // Отправляем на почту
        /** @var \Phalcon\Mailer\Manager $mailer */
        $mailer = $this->di->getShared('mailer');

        $mail = $mailer->createMessageFromView(
                'view_path',
                ['form_data' => $form_data, 'contract_data' => $contract_data]
            )
            ->to($contract_data['manager']['work_email'])
            ->subject($this->t->_('mailer.juridical_dms_add_insured_theme'));

        $count = $mail->send();

        $this->logger->log(
            Logger::INFO,
            "Отправка письма\n"
            . "Текст письма: {content}\n"
            . "Email: {curator_email}\n"
            . "Кол-во отправленных писем: {status}\n",
            [
                'content' => $mail->getContent(),
                'curator_email' => $contract_data['manager']['work_email'],
                'status' => $count
            ]
        );

        if (!$count) {
            throw new UserableException($this->t->_('mailer.failed_to_send_email'));
        }
}

And, let's say, you need to do the same action, only replace the path to the view_path of the letter. Do not copy-paste

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question