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

ResourceRepositoryContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
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\ResourceRepository\Context;
13
14
use FSi\Bundle\AdminBundle\Admin\Context\ContextAbstract;
15
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface;
16
use FSi\Bundle\AdminBundle\Admin\Element;
17
use FSi\Bundle\AdminBundle\Event\AdminEvent;
18
use FSi\Bundle\AdminBundle\Event\FormEvent;
19
use FSi\Bundle\AdminBundle\Admin\ResourceRepository\Element as ResourceRepositoryElement;
20
use FSi\Bundle\AdminBundle\Admin\ResourceRepository\ResourceFormBuilder;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
24
class ResourceRepositoryContext extends ContextAbstract
25
{
26
    /**
27
     * @var ResourceRepositoryElement
28
     */
29
    protected $element;
30
31
    /**
32
     * @var ResourceFormBuilder
33
     */
34
    private $resourceFormBuilder;
35
36
    /**
37
     * @var FormInterface
38
     */
39
    private $form;
40
41
    /**
42
     * @param HandlerInterface[] $requestHandlers
43
     * @param string $template
44
     * @param ResourceFormBuilder $resourceFormBuilder
45
     */
46
    public function __construct(
47
        array $requestHandlers,
48
        string $template,
49
        ResourceFormBuilder $resourceFormBuilder
50
    ) {
51
        parent::__construct($requestHandlers, $template);
52
53
        $this->resourceFormBuilder = $resourceFormBuilder;
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...urceRepository\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
        $this->form = $this->resourceFormBuilder->build($this->element);
0 ignored issues
show
Compatibility introduced by
$this->element of type object<FSi\Bundle\AdminBundle\Admin\Element> is not a sub-type of object<FSi\Bundle\AdminB...urceRepository\Element>. It seems like you assume a child interface of the interface FSi\Bundle\AdminBundle\Admin\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
    }
61
62
    public function hasTemplateName(): bool
63
    {
64
        return $this->element->hasOption('template') || parent::hasTemplateName();
65
    }
66
67
    public function getTemplateName(): string
68
    {
69
        return $this->element->hasOption('template')
70
            ? $this->element->getOption('template')
71
            : parent::getTemplateName()
72
        ;
73
    }
74
75
    public function getData(): array
76
    {
77
        return [
78
            'form' => $this->form->createView(),
79
            'element' => $this->element
80
        ];
81
    }
82
83
    protected function createEvent(Request $request): AdminEvent
84
    {
85
        return new FormEvent($this->element, $request, $this->form);
86
    }
87
88
    protected function getSupportedRoute(): string
89
    {
90
        return 'fsi_admin_resource';
91
    }
92
93
    protected function supportsElement(Element $element): bool
94
    {
95
        return $element instanceof ResourceRepositoryElement;
96
    }
97
}
98