EntityFieldsFormBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\UI\Field;
4
5
use DB;
6
use Hechoenlaravel\JarvisFoundation\EntityGenerator\EntityModel;
7
8
/**
9
 * Builds the fields HTML for a form
10
 * Class EntityFieldsFormBuilder
11
 * @package Hechoenlaravel\JarvisFoundation\UI\Field
12
 */
13
class EntityFieldsFormBuilder
14
{
15
    /**
16
     * @var EntityModel
17
     */
18
    public $entity;
19
20
    /**
21
     * @var \Illuminate\Foundation\Application|mixed
22
     */
23
    protected $typeResolver;
24
25
    /**
26
     * @var array
27
     */
28
    protected $types = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $presenters = [];
34
35
    /**
36
     * @param EntityModel $entity
37
     */
38
    public function __construct(EntityModel $entity)
39
    {
40
        $this->entity = $entity;
41
        $this->typeResolver = app('field.types');
42
        $this->setFieldTypes();
43
    }
44
45 View Code Duplication
    public function setRowId($id, $column = "id")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $this->entry = DB::table($this->entity->getTableName())->where($column, $id)->first();
0 ignored issues
show
Bug introduced by
The property entry does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
        foreach ($this->types as $field) {
49
            $field->setValue($this->entry->{$field->fieldSlug});
50
        }
51
    }
52
53
    /**
54
     * @return $this
55
     */
56
    public function render()
57
    {
58
        return view('jarvisPlatform::field.form')
0 ignored issues
show
Bug introduced by
The method with() does not seem to exist on object<BladeView>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
            ->with('fields', $this->types);
60
    }
61
62
    /**
63
     * @return $this
64
     */
65
    protected function setFieldTypes()
66
    {
67
        $i = 0;
68
        foreach ($this->entity->fields as $field) {
69
            $this->types[$i] = $this->typeResolver->getFieldClass($field->type);
70
            $this->types[$i]->fieldSlug = $field->slug;
71
            $this->types[$i]->fieldName = $field->name;
72
            $this->types[$i]->fieldDescription = $field->description;
73
            $this->types[$i]->fieldOptions = $field->options;
74
            $i++;
75
        }
76
77
        return $this;
78
    }
79
}
80