Completed
Pull Request — master (#311)
by Łukasz
06:56
created

DisplayContext::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * (c) FSi sp. z o.o. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
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 object<FSi\Bundle\AdminBundle\Admin\Element>, but the property $element was declared to be of type object<FSi\Bundle\AdminB...\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
        $this->display = $this->element->createDisplay($this->getObject($request));
64
65
        return new DisplayEvent($this->element, $request, $this->display);
66
    }
67
68
    protected function getSupportedRoute(): string
69
    {
70
        return 'fsi_admin_display';
71
    }
72
73
    protected function supportsElement(Element $element): bool
74
    {
75
        return $element instanceof DisplayElement;
76
    }
77
78
    /**
79
     * @param Request $request
80
     * @return mixed
81
     */
82
    private function getObject(Request $request)
83
    {
84
        $id = $request->get('id');
85
86
        $object = $this->element->getDataIndexer()->getData($id);
87
        if (!$object) {
88
            throw new NotFoundHttpException(sprintf('Can\'t find object with id %s', $id));
89
        }
90
91
        return $object;
92
    }
93
}
94