Passed
Pull Request — master (#17)
by Theo
01:41
created

CalendarController::dispatchWithBC()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 2
crap 2.0625
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)
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);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $eventName. ( Ignorable by Annotation )

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

60
            return $this->eventDispatcher->/** @scrutinizer ignore-call */ dispatch($event, $eventName);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
61
        }
62
63
        return $this->eventDispatcher->dispatch($eventName, $event);
64
    }
65
}
66