Completed
Pull Request — master (#259)
by Piotr
03:21
created

FormElementContext   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 108
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hasTemplateName() 0 4 1
A getTemplateName() 0 4 1
A getData() 0 7 1
A setElement() 0 4 1
A createEvent() 0 6 1
A getSupportedRoute() 0 4 1
A supportsElement() 0 4 1
A getObject() 0 15 3
A checkAllowAddOption() 0 11 3
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
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context;
11
12
use FSi\Bundle\AdminBundle\Admin\Context\ContextAbstract;
13
use FSi\Bundle\AdminBundle\Admin\CRUD\FormElement;
14
use FSi\Bundle\AdminBundle\Admin\Element;
15
use FSi\Bundle\AdminBundle\Event\FormEvent;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
20
class FormElementContext extends ContextAbstract
21
{
22
    /**
23
     * @var FormElement
24
     */
25
    protected $element;
26
27
    /**
28
     * @var FormInterface
29
     */
30
    protected $form;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function hasTemplateName()
36
    {
37
        return $this->element->hasOption('template_form');
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getTemplateName()
44
    {
45
        return $this->element->getOption('template_form');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getData()
52
    {
53
        return array(
54
            'form' => $this->form->createView(),
55
            'element' => $this->element,
56
        );
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function setElement(Element $element)
63
    {
64
        $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\CRUD\FormElement>. 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...
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function createEvent(Request $request)
71
    {
72
        $this->form = $this->element->createForm($this->getObject($request));
73
74
        return new FormEvent($this->element, $request, $this->form);
0 ignored issues
show
Bug introduced by
It seems like $this->form can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    protected function getSupportedRoute()
81
    {
82
        return 'fsi_admin_form';
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function supportsElement(Element $element)
89
    {
90
        return $element instanceof FormElement;
91
    }
92
93
    /**
94
     * @param Request $request
95
     * @return object|null
96
     */
97
    private function getObject(Request $request)
98
    {
99
        $id = $request->get('id');
100
        if (empty($id)) {
101
            $this->checkAllowAddOption();
102
            return null;
103
        }
104
105
        $object = $this->element->getDataIndexer()->getData($id);
106
        if (!$object) {
107
            throw new NotFoundHttpException(sprintf('Can\'t find object with id %s', $id));
108
        }
109
110
        return $object;
111
    }
112
113
    /**
114
     * @throws NotFoundHttpException
115
     */
116
    private function checkAllowAddOption()
117
    {
118
        if ($this->element->hasOption('allow_add')
119
            && !$this->element->getOption('allow_add')
120
        ) {
121
            throw new NotFoundHttpException(sprintf(
122
                'Cannot add objects through element "%s", because it has option "allow_add" set to false',
123
                get_class($this->element)
124
            ));
125
        }
126
    }
127
}
128