ControllerAbstract::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Controller;
13
14
use FSi\Bundle\AdminBundle\Admin\Context\ContextManager;
15
use FSi\Bundle\AdminBundle\Admin\Element;
16
use FSi\Bundle\AdminBundle\Event\AdminEvent;
17
use FSi\Bundle\AdminBundle\Event\AdminEvents;
18
use FSi\Bundle\AdminBundle\Exception\ContextException;
19
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
25
abstract class ControllerAbstract
26
{
27
    /**
28
     * @var EngineInterface
29
     */
30
    protected $templating;
31
32
    /**
33
     * @var ContextManager
34
     */
35
    protected $contextManager;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    public function __construct(
43
        EngineInterface $templating,
44
        ContextManager $contextManager,
45
        EventDispatcherInterface $eventDispatcher
46
    ) {
47
        $this->templating = $templating;
48
        $this->contextManager = $contextManager;
49
        $this->eventDispatcher = $eventDispatcher;
50
    }
51
52
    protected function handleRequest(Element $element, Request $request, string $route): Response
53
    {
54
        $event = new AdminEvent($element, $request);
55
        $this->eventDispatcher->dispatch(AdminEvents::CONTEXT_PRE_CREATE, $event);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

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

55
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
56
                                dispatch(AdminEvents::CONTEXT_PRE_CREATE, $event);

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...
Bug introduced by
FSi\Bundle\AdminBundle\E...nts::CONTEXT_PRE_CREATE 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

55
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ AdminEvents::CONTEXT_PRE_CREATE, $event);
Loading history...
56
        if ($event->hasResponse()) {
57
            return $event->getResponse();
58
        }
59
60
        $context = $this->contextManager->createContext($route, $element);
61
        if (null === $context) {
62
            throw new NotFoundHttpException(sprintf(
63
                'Cannot find context builder that supports element with id "%s"',
64
                $element->getId()
65
            ));
66
        }
67
68
        $response = $context->handleRequest($request);
69
        if ($response instanceof Response) {
70
            return $response;
71
        }
72
73
        if (!$context->hasTemplateName()) {
74
            throw new ContextException(sprintf(
75
                'Context %s neither returned a response nor has a template name',
76
                get_class($context)
77
            ));
78
        }
79
80
        return $this->templating->renderResponse(
81
            $context->getTemplateName(),
82
            $context->getData()
83
        );
84
    }
85
}
86