ListElementContext::supportsElement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 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\CRUD\Context;
13
14
use FSi\Bundle\AdminBundle\Admin\Context\ContextAbstract;
15
use FSi\Bundle\AdminBundle\Admin\CRUD\ListElement;
16
use FSi\Bundle\AdminBundle\Admin\Element;
17
use FSi\Bundle\AdminBundle\Event\AdminEvent;
18
use FSi\Bundle\AdminBundle\Event\ListEvent;
19
use FSi\Component\DataGrid\DataGridInterface;
20
use FSi\Component\DataSource\DataSourceInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
23
class ListElementContext extends ContextAbstract
24
{
25
    /**
26
     * @var ListElement
27
     */
28
    protected $element;
29
30
    /**
31
     * @var DataSourceInterface
32
     */
33
    protected $dataSource;
34
35
    /**
36
     * @var DataGridInterface
37
     */
38
    protected $dataGrid;
39
40
    public function setElement(Element $element): void
41
    {
42
        $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\CRUD\ListElement. 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...
43
        $this->dataSource = $this->element->createDataSource();
0 ignored issues
show
Bug introduced by
The method createDataSource() does not exist on FSi\Bundle\AdminBundle\Admin\Element. It seems like you code against a sub-type of FSi\Bundle\AdminBundle\Admin\Element such as FSi\Bundle\AdminBundle\A...CRUD\GenericListElement or FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\ListElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\A...UD\DependentListElement or FSi\Bundle\AdminBundle\D...in\DependentListElement or FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement or FSi\Bundle\AdminBundle\D...in\DependentCRUDElement. ( Ignorable by Annotation )

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

43
        /** @scrutinizer ignore-call */ 
44
        $this->dataSource = $this->element->createDataSource();
Loading history...
44
        $this->dataGrid = $this->element->createDataGrid();
0 ignored issues
show
Bug introduced by
The method createDataGrid() does not exist on FSi\Bundle\AdminBundle\Admin\Element. It seems like you code against a sub-type of FSi\Bundle\AdminBundle\Admin\Element such as FSi\Bundle\AdminBundle\A...CRUD\GenericListElement or FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\ListElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\A...UD\DependentListElement or FSi\Bundle\AdminBundle\D...in\DependentListElement or FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement or FSi\Bundle\AdminBundle\D...in\DependentCRUDElement. ( Ignorable by Annotation )

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

44
        /** @scrutinizer ignore-call */ 
45
        $this->dataGrid = $this->element->createDataGrid();
Loading history...
45
    }
46
47
    public function hasTemplateName(): bool
48
    {
49
        return $this->element->hasOption('template_list') || parent::hasTemplateName();
50
    }
51
52
    public function getTemplateName(): ?string
53
    {
54
        return $this->element->hasOption('template_list')
55
            ? $this->element->getOption('template_list')
56
            : parent::getTemplateName()
57
        ;
58
    }
59
60
    public function getData(): array
61
    {
62
        return [
63
            'datagrid_view' => $this->dataGrid->createView(),
64
            'datasource_view' => $this->dataSource->createView(),
65
            'element' => $this->element,
66
        ];
67
    }
68
69
    protected function createEvent(Request $request): AdminEvent
70
    {
71
        return new ListEvent($this->element, $request, $this->dataSource, $this->dataGrid);
72
    }
73
74
    protected function getSupportedRoute(): string
75
    {
76
        return 'fsi_admin_list';
77
    }
78
79
    protected function supportsElement(Element $element): bool
80
    {
81
        return $element instanceof ListElement;
82
    }
83
}
84