CalendarController::loadAction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 3
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CalendarBundle\Controller;
6
7
use CalendarBundle\CalendarEvents;
8
use CalendarBundle\Event\CalendarEvent;
9
use CalendarBundle\Serializer\SerializerInterface;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
15
16
class CalendarController extends AbstractController
17
{
18
    /**
19
     * @var SerializerInterface
20
     */
21
    protected $serializer;
22
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    protected $eventDispatcher;
27
28 2
    public function __construct(
29
        EventDispatcherInterface $eventDispatcher,
30
        SerializerInterface $serializer
31
    ) {
32 2
        $this->eventDispatcher = $eventDispatcher;
33 2
        $this->serializer = $serializer;
34 2
    }
35
36 2
    public function loadAction(Request $request): Response
37
    {
38 2
        $start = new \DateTime($request->get('start'));
39 2
        $end = new \DateTime($request->get('end'));
40 2
        $filters = $request->get('filters', '{}');
41 2
        $filters = \is_array($filters) ? $filters : json_decode($filters, true);
42
43 2
        $event = $this->dispatchWithBC(
44 2
            new CalendarEvent($start, $end, $filters),
45 2
            CalendarEvents::SET_DATA
46
        );
47 2
        $content = $this->serializer->serialize($event->getEvents());
48
49 2
        $response = new Response();
50 2
        $response->headers->set('Content-Type', 'application/json');
51 2
        $response->setContent($content);
52 2
        $response->setStatusCode(empty($content) ? Response::HTTP_NO_CONTENT : Response::HTTP_OK);
53
54 2
        return $response;
55
    }
56
57 2
    public function dispatchWithBC($event, ?string $eventName = null)
58
    {
59 2
        if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
0 ignored issues
show
introduced by
$this->eventDispatcher is always a sub-type of Symfony\Contracts\EventD...ventDispatcherInterface.
Loading history...
60 2
            return $this->eventDispatcher->dispatch($event, $eventName);
61
        }
62
63
        return $this->eventDispatcher->dispatch($eventName, $event);
64
    }
65
}
66