Marcius Leandro
Marcius Leandro

Reputation: 789

Create a service in symfony2

I have two very similar classes that are treating questions in an ads. Currently they are separated by treating the API and the other on the web. I would like to create a service that would make the validation and save the bank and asks me to return any key.

WEB

public function createAction(Request $request, $adId)
{
    $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');

    $em = $this->getDoctrine()->getManager();

    $ad = $em->getRepository('DelivveWebBundle:Ad')->findOneBy(array('id' => $adId));
    $author = $this->getUser();

    $question = new Question();
    $question->setAd($ad);
    $question->setAuthor($author);

    $form_question = $this->createQuestionForm($question, $adId);
    $form_question->handleRequest($request);

    if ($form_question->isValid()) {
        $em->persist($question);

        $notification = new Notification();
        $notification->setType(Constant::NOTIFY_QUESTION);
        $notification->setTarget($question->getId());
        $notification->setUser($ad->getOwner());
        $notification->setAgent($author);

        $em->persist($notification);

        $em->flush();

        return $this->redirect($this->generateUrl('show_ad', array('id' => $ad->getId())) . '#question' . $question->getId());
    }

    return $this->redirect($this->generateUrl('show_ad', array('id' => $ad->getId())));
}

API

public function postAdQuestionAction(ParamFetcher $paramFetcher, $id)
{
    /** @var EntityManager $em */
    $em = $this->getDoctrine()->getManager();

    /** @var Ad $ad */
    $ad = $this->getDoctrine()->getRepository('DelivveWebBundle:Ad')->find($id);

    /** @var User $author */
    $author = $this->getUser();

    if ($ad) {
        if ($ad->getStatus() == Constant::AD_NEW) {
            if (!$ad->getOwner()->isEqualTo($this->getUser())) {
                $question = new Question();
                $question->setAuthor($this->getUser());
                $question->setAd($ad);
                $question->setMessage($paramFetcher->get('message'));
                $em->persist($question);

                $notification = new Notification();
                $notification->setType(Constant::NOTIFY_QUESTION);
                $notification->setTarget($question->getId());
                $notification->setUser($ad->getOwner());
                $notification->setAgent($author);

                $em->persist($notification);

                $em->flush();

                return $this->view(array('status' => 0, 'message' => null), 200);
            } else {
                return $this->view(array('status' => 3, 'message' => $this->get('translator')->trans('not_permitted')), 403);
            }
        } else {
            return $this->view(array('status' => 2, 'message' => $this->get('translator')->trans('ad.closed')), 403);
        }
    }
    else {
        return $this->view(array('status' => 1, 'message' => $this->get('translator')->trans('ad.not_found')), 403);
    }
}

However'm new to symfony and I'm not sure where to start, what I have so far is this:

services.yml

services:
    questions_service:
        class:        Delivve\WebBundle\Services\QuestionsService
        arguments:    ["@doctrine.orm.entity_manager"]

class QuestionsService

class QuestionsService extends Controller{
    /**
     * @var EntityManager
     */
    protected $em;

    public function __construct(EntityManager $entityManager){
        $this->em = $entityManager;
    }

    public function  createQuestion($id, $message){
        /** @var Ad $ad */
        $ad = $this->getDoctrine()->getRepository('DelivveWebBundle:Ad')->find($id);

        /** @var User $author */
        $author = $this->getUser();

        if ($ad) {
            if ($ad->getStatus() == Constant::AD_NEW) {
                if (!$ad->getOwner()->isEqualTo($this->getUser())) {
                    $question = new Question();
                    $question->setAuthor($this->getUser());
                    $question->setAd($ad);
                    $question->setMessage($message);
                    $em->persist($question);

                    $notification = new Notification();
                    $notification->setType(Constant::NOTIFY_QUESTION);
                    $notification->setTarget($question->getId());
                    $notification->setUser($ad->getOwner());
                    $notification->setAgent($author);

                    $em->persist($notification);

                    $em->flush();

                    return 0;
                } else {
                    return 3;
                }
            } else {
                return 2;
            }
        }
        else {
            return 1;
        }
    }
}

The idea is that the two controller would call createQuestion function, but do not know how to do this, from what I read in the references have to make a call but did not get it right, if anyone can help me, thank you now.

Upvotes: 0

Views: 43

Answers (1)

Amine Matmati
Amine Matmati

Reputation: 283

To call a service you only need to get it from the container. If you're inside a controller you can do;

$this->get('questions_service');

So for calling a method:

$this->get('questions_service')->createQuestion($id, $message);

Upvotes: 1

Related Questions