Test Failed
Pull Request — master (#17)
by Theo
01:56
created

CalendarController::dispatchWithBC()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
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\EventDispatcher\LegacyEventDispatcherProxy;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
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
    public function __construct(
29
        EventDispatcherInterface $eventDispatcher,
30
        SerializerInterface $serializer
31
    ) {
32
        if (class_exists(LegacyEventDispatcherProxy::class)) {
33
            $eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
34
        }
35
        $this->eventDispatcher = $eventDispatcher;
36
        $this->serializer = $serializer;
37
    }
38
39
    public function loadAction(Request $request): Response
40
    {
41
        $start = new \DateTime($request->get('start'));
42
        $end = new \DateTime($request->get('end'));
43
        $filters = $request->get('filters', '{}');
44
        $filters = \is_array($filters) ? $filters : json_decode($filters, true);
45
46
        $event = $this->dispatchWithBC(
47
            new CalendarEvent($start, $end, $filters),
48
            CalendarEvents::SET_DATA
49
        );
50
        $content = $this->serializer->serialize($event->getEvents());
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
    private function dispatchWithBC($event, string $eventName)
61
    {
62
        if (class_exists(LegacyEventDispatcherProxy::class)) {
63
            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

63
            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...
64
        }
65
66
        return $this->eventDispatcher->dispatch($eventName, $event);
0 ignored issues
show
Bug introduced by
$eventName of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

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

66
        return $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ $eventName, $event);
Loading history...
67
    }
68
}
69