SinchController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A callbackAction() 0 19 4
1
<?php
2
/*
3
 * This file is part of the FreshSinchBundle
4
 *
5
 * (c) Artem Henvald <[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
declare(strict_types=1);
12
13
namespace Fresh\SinchBundle\Controller;
14
15
use Fresh\SinchBundle\Event\SinchEvents;
16
use Fresh\SinchBundle\Event\SmsMessageCallbackEvent;
17
use Fresh\SinchBundle\Form\Type\CallbackRequestType;
18
use Fresh\SinchBundle\Model\CallbackRequest;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\Form\FormFactory;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
/**
25
 * SinchController.
26
 *
27
 * @author Artem Henvald <[email protected]>
28
 */
29
class SinchController
30
{
31
    /** @var EventDispatcherInterface */
32
    private $eventDispatcher;
33
34
    /** @var FormFactory */
35
    private $formFactory;
36
37
    /**
38
     * @param FormFactory              $formFactory
39
     * @param EventDispatcherInterface $eventDispatcher
40
     */
41
    public function __construct(FormFactory $formFactory, EventDispatcherInterface $eventDispatcher)
42
    {
43
        $this->formFactory = $formFactory;
44
        $this->eventDispatcher = $eventDispatcher;
45
    }
46
47
    /**
48
     * @param Request $request
49
     *
50
     * @return Response
51
     */
52
    public function callbackAction(Request $request): Response
53
    {
54
        try {
55
            $callbackRequest = new CallbackRequest();
56
            $form = $this->formFactory->create(CallbackRequestType::class, $callbackRequest);
57
            $form->handleRequest($request);
58
59
            if ($form->isSubmitted() && $form->isValid()) {
60
                $event = new SmsMessageCallbackEvent($callbackRequest);
61
                $this->eventDispatcher->dispatch(SinchEvents::CALLBACK_RECEIVED, $event);
62
            } else {
63
                return new Response('Bad Request', Response::HTTP_BAD_REQUEST);
64
            }
65
        } catch (\Exception $e) {
66
            return new Response('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR);
67
        }
68
69
        return new Response(null, Response::HTTP_OK);
70
    }
71
}
72