FormManager::getRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Knp\RadBundle\Form;
4
5
use Symfony\Component\HttpFoundation\RequestStack;
6
7
class FormManager
8
{
9
    private $requestStack;
10
    private $creators;
11
12
    public function __construct(RequestStack $requestStack)
13
    {
14
        $this->creators     = new \SplPriorityQueue;
15
        $this->requestStack = $requestStack;
16
    }
17
18
    public function createObjectForm($object, $purpose = null, array $options = array())
19
    {
20
        foreach ($this->getCreators() as $creator) {
21
            if ($form = $creator->create($object, $purpose, $options)) {
22
                return $form;
23
            }
24
        }
25
26
        throw new \RuntimeException(sprintf('The form manager was unable to create the form. Please, make sure you have correctly registered one that fit your need.'));
27
    }
28
29
    public function createBoundObjectForm($object, $purpose = null, array $options = array())
30
    {
31
        if (!$this->getRequest()->isMethodSafe()) {
32
            $options = array_merge(array('method' => $this->getRequest()->getMethod()), $options);
33
        }
34
        $form = $this->createObjectForm($object, $purpose, $options);
35
        $form->handleRequest($this->getRequest());
36
37
        return $form;
38
    }
39
40
    public function registerCreator(FormCreatorInterface $creator, $priority = 0)
41
    {
42
        $this->creators->insert($creator, $priority);
43
    }
44
45
    public function getCreators()
46
    {
47
        return array_values(iterator_to_array(clone $this->creators));
48
    }
49
50
    private function getRequest()
51
    {
52
        return $this->requestStack->getCurrentRequest();
53
    }
54
}
55