Passed
Push — master ( 3f618b...24c7ed )
by Theo
02:15
created

CalendarController::loadAction()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 1
nop 1
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 6
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace CalendarBundle\Controller;
4
5
use CalendarBundle\CalendarEvents;
6
use CalendarBundle\Event\CalendarEvent;
7
use CalendarBundle\Serializer\SerializerInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class CalendarController extends AbstractController
14
{
15
    /**
16
     * @var SerializerInterface
17
     */
18
    protected $serializer;
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    protected $eventDispatcher;
24
25
    /**
26
     * @param EventDispatcherInterface $eventDispatcher
27
     * @param SerializerInterface      $serializer
28
     */
29
    public function __construct(
30
        EventDispatcherInterface $eventDispatcher,
31
        SerializerInterface $serializer
32
    ) {
33
        $this->eventDispatcher = $eventDispatcher;
34
        $this->serializer = $serializer;
35
    }
36
37
    /**
38
     * @param Request $request
39
     *
40
     * @return Response
41
     */
42
    public function loadAction(Request $request): Response
43
    {
44
        $start = new \DateTime($request->get('start'));
45
        $end = new \DateTime($request->get('end'));
46
        $filters = $request->get('filters', []);
47
48
        $event = new CalendarEvent($start, $end, $filters);
49
        $events = $this->eventDispatcher->dispatch(CalendarEvents::SET_DATA, $event)->getEvents();
0 ignored issues
show
Bug introduced by
The method getEvents() does not exist on Symfony\Component\EventDispatcher\Event. It seems like you code against a sub-type of Symfony\Component\EventDispatcher\Event such as CalendarBundle\Event\CalendarEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $events = $this->eventDispatcher->dispatch(CalendarEvents::SET_DATA, $event)->/** @scrutinizer ignore-call */ getEvents();
Loading history...
50
        $content = $this->serializer->serialize($events);
51
52
        $response = new Response();
53
        $response->headers->set('Content-Type', 'application/json');
54
        $response->setContent($content);
55
        $response->setStatusCode(empty($content) ? Response::HTTP_NO_CONTENT : Response::HTTP_OK);
56
57
        return $response;
58
    }
59
}
60