EntityFieldPresenter::setRowId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\UI\Field;
4
5
use DB;
6
use Hechoenlaravel\JarvisFoundation\EntityGenerator\EntityModel;
7
use Hechoenlaravel\JarvisFoundation\Field\FieldTypeInterface;
8
9
10
/**
11
 * Class EntityFieldPresenter
12
 * @package Hechoenlaravel\JarvisFoundation\UI\Field
13
 */
14
class EntityFieldPresenter
15
{
16
    /**
17
     * @var EntityModel
18
     */
19
    public $entity;
20
21
    /**
22
     * @var \Illuminate\Foundation\Application|mixed
23
     */
24
    protected $typeResolver;
25
26
    /**
27
     * @var array
28
     */
29
    protected $types = [];
30
31
    /**
32
     * @var
33
     */
34
    protected $entry;
35
36
    /**
37
     * EntityFieldPresenter constructor.
38
     * @param EntityModel $entity
39
     */
40
    public function __construct(EntityModel $entity)
41
    {
42
        $this->entity = $entity;
43
        $this->typeResolver = app('field.types');
44
        $this->setFieldTypes();
45
    }
46
47
    /**
48
     * @param $id
49
     */
50 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...
51
    {
52
        $this->entry = DB::table($this->entity->getTableName())->where($column, $id)->first();
53
        foreach ($this->types as $field) {
54
            $field->setValue($this->entry->{$field->fieldSlug});
55
        }
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getFields()
62
    {
63
        return $this->types;
64
    }
65
66
    /**
67
     * @return $this
68
     */
69
    protected function setFieldTypes()
70
    {
71
        foreach ($this->entity->fields as $field) {
72
            $this->types[$field->slug] = $this->typeResolver->getFieldClass($field->type);
73
            $this->types[$field->slug]->fieldSlug = $field->slug;
74
            $this->types[$field->slug]->fieldName = $field->name;
75
            $this->types[$field->slug]->fieldDescription = $field->description;
76
            $this->types[$field->slug]->fieldOptions = $field->options;
77
        }
78
        return $this;
79
    }
80
}