Completed
Push — master ( f95c98...4cd325 )
by Kristijan
9s
created

FormBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 2 Features 2
Metric Value
c 9
b 2
f 2
dl 0
loc 102
ccs 39
cts 39
cp 1
rs 10
wmc 6
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getNamespaceFromConfig() 0 10 2
B create() 0 26 2
A plain() 0 16 1
1
<?php  namespace Kris\LaravelFormBuilder;
2
3
use Illuminate\Contracts\Container\Container;
4
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
5
use Kris\LaravelFormBuilder\Events\AfterFormCreation;
6
7
class FormBuilder
8
{
9
10
    /**
11
     * @var Container
12
     */
13
    protected $container;
14
15
    /**
16
     * @var FormHelper
17
     */
18
    protected $formHelper;
19
20
    /**
21
     * @var EventDispatcher
22
     */
23
    protected $eventDispatcher;
24
25
    /**
26
     * @param Container  $container
27
     * @param FormHelper $formHelper
28
     */
29 101
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
30
    {
31 101
        $this->container = $container;
32 101
        $this->formHelper = $formHelper;
33 101
        $this->eventDispatcher = $eventDispatcher;
34 101
    }
35
36
    /**
37
     * @param       $formClass
38
     * @param       $options
39
     * @param       $data
40
     * @return Form
41
     */
42 14
    public function create($formClass, array $options = [], array $data = [])
43
    {
44 14
        $class = $this->getNamespaceFromConfig() . $formClass;
45
46 14
        if (!class_exists($class)) {
47 1
            throw new \InvalidArgumentException(
48 1
                'Form class with name ' . $class . ' does not exist.'
49
            );
50
        }
51
52 13
        $form = $this->container
53 13
            ->make($class)
54 13
            ->addData($data)
55 13
            ->setRequest($this->container->make('request'))
56 13
            ->setFormHelper($this->formHelper)
57 13
            ->setEventDispatcher($this->eventDispatcher)
58 13
            ->setFormBuilder($this)
59 13
            ->setValidator($this->container->make('validator'))
60 13
            ->setFormOptions($options);
61
62 13
        $form->buildForm();
63
64 13
        $this->eventDispatcher->fire(new AfterFormCreation($form));
65
66 13
        return $form;
67
    }
68
69
    /**
70
     * Get the namespace from the config
71
     *
72
     * @return string
73
     */
74 14
    protected function getNamespaceFromConfig()
75
    {
76 14
        $namespace = $this->formHelper->getConfig('default_namespace');
77
78 14
        if (!$namespace) {
79 13
            return '';
80
        }
81
82 1
        return $namespace . '\\';
83
    }
84
85
    /**
86
     * Get instance of the empty form which can be modified
87
     *
88
     * @param array $options
89
     * @param array $data
90
     * @return Form
91
     */
92 101
    public function plain(array $options = [], array $data = [])
93
    {
94 101
        $form = $this->container
95 101
            ->make('Kris\LaravelFormBuilder\Form')
96 101
            ->addData($data)
97 101
            ->setRequest($this->container->make('request'))
98 101
            ->setFormHelper($this->formHelper)
99 101
            ->setEventDispatcher($this->eventDispatcher)
100 101
            ->setFormBuilder($this)
101 101
            ->setValidator($this->container->make('validator'))
102 101
            ->setFormOptions($options);
103
104 101
        $this->eventDispatcher->fire(new AfterFormCreation($form));
105
106 101
        return $form;
107
    }
108
}
109