Completed
Push — master ( ceb694...183c93 )
by Rudie
01:46
created

FormBuilder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.38%

Importance

Changes 0
Metric Value
dl 0
loc 203
ccs 54
cts 64
cp 0.8438
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fireEvent() 0 4 1
A create() 0 20 2
B buildFormByArray() 0 17 7
A getNamespaceFromConfig() 0 10 2
A getFormClass() 0 3 1
A setFormClass() 0 8 2
A setDependenciesAndOptions() 0 11 1
A createByArray() 0 16 1
A plain() 0 14 1
1
<?php
2
3
namespace Kris\LaravelFormBuilder;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
7
use Kris\LaravelFormBuilder\Events\AfterFormCreation;
8
9
class FormBuilder
10
{
11
12
    /**
13
     * @var Container
14
     */
15
    protected $container;
16
17
    /**
18
     * @var FormHelper
19
     */
20
    protected $formHelper;
21
22
    /**
23
     * @var EventDispatcher
24
     */
25
    protected $eventDispatcher;
26
27
    /**
28
     * @param Container $container
29
     * @var string
30
     */
31
    protected $plainFormClass = Form::class;
32
33
    /**
34
     * @param Container  $container
35
     * @param FormHelper $formHelper
36
     * @param EventDispatcher $eventDispatcher
37
     */
38 133
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
39
    {
40 133
        $this->container = $container;
41 133
        $this->formHelper = $formHelper;
42 133
        $this->eventDispatcher = $eventDispatcher;
43 133
    }
44
45
    /**
46
     * Fire an event.
47
     *
48
     * @param object $event
49
     * @return array|null
50
     */
51 10
    public function fireEvent($event)
52
    {
53 10
        return $this->eventDispatcher->dispatch($event);
54
    }
55
56
    /**
57
     * Create a Form instance.
58
     *
59
     * @param string $formClass The name of the class that inherits \Kris\LaravelFormBuilder\Form.
60
     * @param array $options|null
0 ignored issues
show
Documentation introduced by
There is no parameter named $options|null. Did you maybe mean $options?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
61
     * @param array $data|null
0 ignored issues
show
Bug introduced by
There is no parameter named $data|null. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     * @return Form
63
     */
64 18
    public function create($formClass, array $options = [], array $data = [])
65
    {
66 18
        $class = $this->getNamespaceFromConfig() . $formClass;
67
68 18
        if (!class_exists($class)) {
69 1
            throw new \InvalidArgumentException(
70 1
                'Form class with name ' . $class . ' does not exist.'
71
            );
72
        }
73
74 17
        $form = $this->setDependenciesAndOptions($this->container->make($class), $options, $data);
75
76 17
        $form->buildForm();
77
78 17
        $this->eventDispatcher->dispatch(new AfterFormCreation($form));
79
80 17
        $form->filterFields();
81
82 17
        return $form;
83
    }
84
85
    /**
86
     * @param $items
87
     * @param array $options
88
     * @param array $data
89
     * @return mixed
90
     */
91 1
    public function createByArray($items, array $options = [], array $data = [])
92
    {
93 1
        $form = $this->setDependenciesAndOptions(
94 1
            $this->container->make($this->plainFormClass),
95
            $options,
96
            $data
97
        );
98
99 1
        $this->buildFormByArray($form, $items);
100
101 1
        $this->eventDispatcher->dispatch(new AfterFormCreation($form));
102
103 1
        $form->filterFields();
104
105 1
        return $form;
106
    }
107
108
    /**
109
     * @param $form
110
     * @param $items
111
     */
112 1
    public function buildFormByArray($form, $items)
113
    {
114 1
        foreach ($items as $item) {
115 1
            if (!isset($item['name'])) {
116
                throw new \InvalidArgumentException(
117
                    'Name is not set in form array.'
118
                );
119
            }
120 1
            $name = $item['name'];
121 1
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
122 1
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
123 1
            unset($item['name']);
124 1
            unset($item['type']);
125 1
            unset($item['modify']);
126 1
            $form->add($name, $type, $item, $modify);
127
        }
128 1
    }
129
130
    /**
131
     * Get the namespace from the config
132
     *
133
     * @return string
134
     */
135 18
    protected function getNamespaceFromConfig()
136
    {
137 18
        $namespace = $this->formHelper->getConfig('default_namespace');
138
139 18
        if (!$namespace) {
140 17
            return '';
141
        }
142
143 1
        return $namespace . '\\';
144
    }
145
146
    /**
147
     * Get instance of the empty form which can be modified
148
     * Get the plain form class.
149
     *
150
     * @return string
151
     */
152
    public function getFormClass() {
153
        return $this->plainFormClass;
154
    }
155
156
    /**
157
     * Set the plain form class.
158
     *
159
     * @param string $class
160
     */
161
    public function setFormClass($class) {
162
        $parent = Form::class;
163
        if (!is_a($class, $parent, true)) {
164
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
165
        }
166
167
        $this->plainFormClass = $class;
168
    }
169
170
    /**
171
     * Get instance of the empty form which can be modified.
172
     *
173
     * @param array $options
174
     * @param array $data
175
     * @return \Kris\LaravelFormBuilder\Form
176
     */
177 133
    public function plain(array $options = [], array $data = [])
178
    {
179 133
        $form = $this->setDependenciesAndOptions(
180 133
            $this->container->make($this->plainFormClass),
181
            $options,
182
            $data
183
        );
184
185 133
        $this->eventDispatcher->dispatch(new AfterFormCreation($form));
186
187 133
        $form->filterFields();
188
189 133
        return $form;
190
    }
191
192
    /**
193
     * Set depedencies and options on existing form instance
194
     *
195
     * @param \Kris\LaravelFormBuilder\Form $instance
196
     * @param array $options
197
     * @param array $data
198
     * @return \Kris\LaravelFormBuilder\Form
199
     */
200 133
    public function setDependenciesAndOptions($instance, array $options = [], array $data = [])
201
    {
202
        return $instance
0 ignored issues
show
Deprecated Code introduced by
The method Kris\LaravelFormBuilder\Form::addData() has been deprecated with message: deprecated since 1.6.12, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data
will be switched to protected in 1.7.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
203 133
            ->addData($data)
204 133
            ->setRequest($this->container->make('request'))
205 133
            ->setFormHelper($this->formHelper)
206 133
            ->setEventDispatcher($this->eventDispatcher)
207 133
            ->setFormBuilder($this)
208 133
            ->setValidator($this->container->make('validator'))
209 133
            ->setFormOptions($options);
210
    }
211
}
212