FormManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 48
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createObjectForm() 0 10 3
A registerCreator() 0 4 1
A getCreators() 0 4 1
A __construct() 0 5 1
A createBoundObjectForm() 0 10 2
A getRequest() 0 4 1
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