EntityFieldPresenter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 10.45 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 7
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setRowId() 7 7 2
A getFields() 0 4 1
A setFieldTypes() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}