G
G
gitdev2021-01-18 22:15:46
symfony
gitdev, 2021-01-18 22:15:46

About code review on Symfony. What needs to be done in services?

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

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