DisplayContext::createEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Admin\Display\Context;
13
14
use FSi\Bundle\AdminBundle\Admin\Context\ContextAbstract;
15
use FSi\Bundle\AdminBundle\Admin\Display\Element as DisplayElement;
16
use FSi\Bundle\AdminBundle\Admin\Element;
17
use FSi\Bundle\AdminBundle\Display\Display;
18
use FSi\Bundle\AdminBundle\Event\AdminEvent;
19
use FSi\Bundle\AdminBundle\Event\DisplayEvent;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
23
class DisplayContext extends ContextAbstract
24
{
25
    /**
26
     * @var DisplayElement
27
     */
28
    protected $element;
29
30
    /**
31
     * @var Display
32
     */
33
    private $display;
34
35
    public function hasTemplateName(): bool
36
    {
37
        return $this->element->hasOption('template') || parent::hasTemplateName();
38
    }
39
40
    public function getTemplateName(): ?string
41
    {
42
        return $this->element->hasOption('template')
43
            ? $this->element->getOption('template')
44
            : parent::getTemplateName()
45
        ;
46
    }
47
48
    public function getData(): array
49
    {
50
        return [
51
            'display' => $this->display->getData(),
52
            'element' => $this->element,
53
        ];
54
    }
55
56
    public function setElement(Element $element): void
57
    {
58
        $this->element = $element;
0 ignored issues
show
Documentation Bug introduced by
$element is of type FSi\Bundle\AdminBundle\Admin\Element, but the property $element was declared to be of type FSi\Bundle\AdminBundle\Admin\Display\Element. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
59
    }
60
61
    protected function createEvent(Request $request): AdminEvent
62
    {
63
        $object = $this->getObject($request);
64
        $this->display = $this->element->createDisplay($object);
65
66
        return new DisplayEvent($this->element, $request, $this->display, $object);
67
    }
68
69
    protected function getSupportedRoute(): string
70
    {
71
        return 'fsi_admin_display';
72
    }
73
74
    protected function supportsElement(Element $element): bool
75
    {
76
        return $element instanceof DisplayElement;
77
    }
78
79
    /**
80
     * @param Request $request
81
     * @return mixed
82
     */
83
    private function getObject(Request $request)
84
    {
85
        $id = $request->get('id');
86
87
        $object = $this->element->getDataIndexer()->getData($id);
88
        if (!$object) {
89
            throw new NotFoundHttpException(sprintf('Can\'t find object with id %s', $id));
90
        }
91
92
        return $object;
93
    }
94
}
95