Completed
Pull Request — master (#328)
by Rudie
04:47
created

FormBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 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
     * @var string
29
     */
30
    protected $plainFormClass = Form::class;
31
32
    /**
33
     * @param Container  $container
34
     * @param FormHelper $formHelper
35
     * @param EventDispatcher $eventDispatcher
36
     */
37 106
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
38
    {
39 106
        $this->container = $container;
40 106
        $this->formHelper = $formHelper;
41 106
        $this->eventDispatcher = $eventDispatcher;
42 106
    }
43
44
    /**
45
     * Create a Form instance.
46
     *
47
     * @param string $formClass The name of the class that inherits \Kris\LaravelFormBuilder\Form.
48
     * @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...
49
     * @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...
50
     * @return Form
51
     */
52 16
    public function create($formClass, array $options = [], array $data = [])
53
    {
54 16
        $class = $this->getNamespaceFromConfig() . $formClass;
55
56 16
        if (!class_exists($class)) {
57 1
            throw new \InvalidArgumentException(
58 1
                'Form class with name ' . $class . ' does not exist.'
59
            );
60
        }
61
62 15
        $form = $this->container
63 15
            ->make($class)
64 15
            ->addData($data)
65 15
            ->setRequest($this->container->make('request'))
66 15
            ->setFormHelper($this->formHelper)
67 15
            ->setEventDispatcher($this->eventDispatcher)
68 15
            ->setFormBuilder($this)
69 15
            ->setValidator($this->container->make('validator'))
70 15
            ->setFormOptions($options);
71
72 15
        $form->buildForm();
73
74 15
        $this->eventDispatcher->fire(new AfterFormCreation($form));
75
76 15
        return $form;
77
    }
78
79
    /**
80
     * Get the namespace from the config.
81
     *
82
     * @return string
83
     */
84 16
    protected function getNamespaceFromConfig()
85
    {
86 16
        $namespace = $this->formHelper->getConfig('default_namespace');
87
88 16
        if (!$namespace) {
89 15
            return '';
90
        }
91
92 1
        return $namespace . '\\';
93
    }
94
95
    /**
96
     * Get the plain form class.
97
     *
98
     * @return string
99
     */
100
    public function getFormClass() {
101
        return $this->plainFormClass;
102
    }
103
104
    /**
105
     * Set the plain form class.
106
     *
107
     * @param string $class
108
     */
109
    public function setFormClass($class) {
110
        $parent = Form::class;
111
        if (!is_a($class, $parent, true)) {
112
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
113
        }
114
115
        $this->plainFormClass = $class;
116
    }
117
118
    /**
119
     * Get instance of the empty form which can be modified.
120
     *
121
     * @param array $options
122
     * @param array $data
123
     * @return \Kris\LaravelFormBuilder\Form
124
     */
125 106
    public function plain(array $options = [], array $data = [])
126
    {
127 106
        $form = $this->container
128 106
            ->make($this->plainFormClass)
129 106
            ->addData($data)
130 106
            ->setRequest($this->container->make('request'))
131 106
            ->setFormHelper($this->formHelper)
132 106
            ->setEventDispatcher($this->eventDispatcher)
133 106
            ->setFormBuilder($this)
134 106
            ->setValidator($this->container->make('validator'))
135 106
            ->setFormOptions($options);
136
137 106
        $this->eventDispatcher->fire(new AfterFormCreation($form));
138
139 106
        return $form;
140
    }
141
}
142