Задать вопрос
@gitdev

Насчет review кода на Symfony. Что нужно вынести в сервисы?

public function new(Request $request, Vacancy $vacancy, PropositionService $propositionService): Response
    {
        /** @var User $user */
        $user = $this->getUser();

        if (!$user) {
            throw new Exception('Only authorized users can create proposition');
        }
        if (!$vacancy) {
            throw new Exception('Expected class Vacancy for create proposition');
        }
        if ($propositionService->checkCountPropositions($user)) {
            $this->addFlash('success', 'You have reached the limit of daily Proposition.');

            return $this->redirectToRoute('propositions');
        }

        $proposition = new Proposition();
        $form = $this->createForm(PropositionType::class, $proposition);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $proposition->setUser($user)
                ->setVacancy($vacancy);
            $proposition->setUrl(rand(1, 10000) . uniqid());

            $this->em->persist($proposition);
            $this->em->flush();

            $propositionService->send($proposition);

            $this->addFlash('success', 'Proposition created');

            return $this->redirectToRoute('propositions');
        }

        return $this->render('proposition/new.html.twig', [
            'proposition' => $proposition,
            'form' => $form->createView(),
        ]);
    }


/**
     * @Route("/", name="propositions", methods={"GET"})
     * @param Request $request
     * @param PropositionRepository $propositionRepository
     * @param PaginatorInterface $paginator
     * @return Response
     */
    public function index(Request $request, PropositionRepository $propositionRepository, PaginatorInterface $paginator): Response
    {
        /** @var User $user */
        $user = $this->getUser();
        $data = null;
        $isFiltersSent = $request->get('filterField') && $request->get('filterValue');
        $filters = [
            'filterField' => $request->get('filterField') ?? null,
            'filterValue' => $request->get('filterValue') ?? null,
        ];

        if ($isFiltersSent) {
            $data = $this->isGranted(User::ROLES['GUEST']) ?
                $propositionRepository->getReferralPropositions($user, $filters) :
                $propositionRepository->getRecruiterPropositions($user, $filters);
        } else {
            $data = $this->isGranted(User::ROLES['GUEST']) ?
                $propositionRepository->getReferralPropositions($user) :
                $propositionRepository->getRecruiterPropositions($user);
        }

        $propositions = $paginator->paginate(
            $data,
            $request->query->getInt('page', 1),
            5, [
                'defaultSortFieldName' => 'vacancy',
                'defaultSortDirection' => 'asc',
            ]
        );

        return $this->render('proposition/index.html.twig', [
            'propositions' => $propositions,
        ]);
    }
  • Вопрос задан
  • 71 просмотр
Подписаться 2 Простой 4 комментария
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы