Completed
Push — master ( bcc292...c4c163 )
by Artem
03:36
created

SinchController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/*
3
 * This file is part of the FreshSinchBundle
4
 *
5
 * (c) Artem Genvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fresh\SinchBundle\Controller;
12
13
use Fresh\SinchBundle\Event\SinchEvents;
14
use Fresh\SinchBundle\Event\SmsMessageCallbackEvent;
15
use Fresh\SinchBundle\Form\Type\CallbackRequestType;
16
use Fresh\SinchBundle\Model\CallbackRequest;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Component\Form\FormFactory;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * SinchController.
24
 *
25
 * @author Artem Genvald <[email protected]>
26
 */
27
class SinchController
28
{
29
    /**
30
     * @var EventDispatcherInterface $eventDispatcher Event dispatcher
31
     */
32
    private $eventDispatcher;
33
34
    /**
35
     * @var FormFactory $formFactory Form factory
36
     */
37
    private $formFactory;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param FormFactory              $formFactory     Form factory
43
     * @param EventDispatcherInterface $eventDispatcher Event dispatcher
44
     */
45
    public function __construct(FormFactory $formFactory, EventDispatcherInterface $eventDispatcher)
46
    {
47
        $this->formFactory     = $formFactory;
48
        $this->eventDispatcher = $eventDispatcher;
49
    }
50
51
    /**
52
     * Callback action.
53
     *
54
     * @param Request $request Request
55
     *
56
     * @return Response
57
     */
58
    public function callbackAction(Request $request)
59
    {
60
        try {
61
            $callbackRequest = new CallbackRequest();
62
            $form = $this->formFactory->create(CallbackRequestType::class, $callbackRequest);
63
            $form->handleRequest($request);
64
65
            if ($form->isSubmitted() && $form->isValid()) {
66
                $event = new SmsMessageCallbackEvent($callbackRequest);
67
                $this->eventDispatcher->dispatch(SinchEvents::CALLBACK_RECEIVED, $event);
68
            } else {
69
                return new Response('Bad Request', Response::HTTP_BAD_REQUEST);
70
            }
71
        } catch (\Exception $e) {
72
            return new Response('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR);
73
        }
74
75
        return new Response(null, Response::HTTP_OK);
76
    }
77
}
78