ResourceRepositoryContext   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 18
dl 0
loc 72
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setElement() 0 4 1
A hasTemplateName() 0 3 2
A getSupportedRoute() 0 3 1
A supportsElement() 0 3 1
A createEvent() 0 3 1
A __construct() 0 8 1
A getData() 0 5 1
A getTemplateName() 0 5 2
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\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 FSi\Bundle\AdminBundle\Admin\Element, but the property $element was declared to be of type FSi\Bundle\AdminBundle\A...ourceRepository\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);
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')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->element->h...rent::getTemplateName() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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