Completed
Pull Request — master (#74)
by
unknown
13:28 queued 09:37
created

Relation   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 9.16 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 64%

Importance

Changes 0
Metric Value
dl 12
loc 131
ccs 32
cts 50
cp 0.64
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
B __construct() 0 27 6
A addSecondaryRelationFields() 0 11 3
A getName() 0 4 1
A getDisplay() 0 6 2
A getPresentation() 12 12 2
A getModelAbstractor() 0 4 1
A getSecondaryRelations() 0 4 1
A setRelatedModel() 0 6 1

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 Anavel\Crud\Abstractor\Eloquent\Relation;
4
5
use ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager;
6
use Anavel\Crud\Abstractor\ConfigurationReader;
7
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationConfig;
8
use Anavel\Crud\Abstractor\Eloquent\Traits\ModelFields;
9
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldFactoryContract;
10
use Anavel\Crud\Contracts\Abstractor\Model as ModelAbstractor;
11
use Anavel\Crud\Contracts\Abstractor\Relation as RelationAbstractorContract;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
14
use Illuminate\Support\Collection;
15
16
abstract class Relation implements RelationAbstractorContract
17
{
18
    use CheckRelationConfig;
19
    use ConfigurationReader;
20
    use ModelFields;
21
22
    const DISPLAY_TYPE_TAB = 'tab';
23
    const DISPLAY_TYPE_INLINE = 'inline';
24
25
    protected $name;
26
    protected $slug;
27
    protected $presentation;
28
    protected $type;
29
    /**
30
     * @var Model
31
     */
32
    protected $relatedModel;
33
    /**
34
     * @var EloquentRelation
35
     */
36
    protected $eloquentRelation;
37
    /**
38
     * @var FieldFactoryContract
39
     */
40
    protected $fieldFactory;
41
    protected $modelManager;
42
    /** @var ModelAbstractor */
43
    protected $modelAbstractor;
44
    /**
45
     * @var array
46
     */
47
    protected $config;
48
49 35
    public function __construct(array $config, ModelManager $modelManager, Model $model, EloquentRelation $eloquentRelation, FieldFactoryContract $fieldFactory)
50
    {
51 35
        $this->checkNameConfig($config);
52 35
        $this->name = $config['name'];
53 35
        $this->slug = $config['name'];
54 35
        $this->relatedModel = $model;
55 35
        $this->eloquentRelation = $eloquentRelation;
56 35
        $this->fieldFactory = $fieldFactory;
57
58 35
        $this->modelManager = $modelManager;
59
60 35
        $this->config = $config;
61 35
        $this->setup();
62
63 32
        $relatedModelClassName = get_class($this->eloquentRelation->getRelated());
64 32
        $relatedmodelRelationsConfig = [];
65
66 32
        foreach (config('anavel-crud.models') as $modelConfig) {
67
            if (is_array($modelConfig) && array_key_exists('model', $modelConfig) && $relatedModelClassName == $modelConfig['model']) {
68
                if (array_key_exists('relations', $modelConfig)) {
69
                    $relatedmodelRelationsConfig['relations'] = $modelConfig['relations'];
70
                }
71
            }
72 32
        }
73
74 32
        $this->modelAbstractor = \App::make('Anavel\Crud\Contracts\Abstractor\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $relatedmodelRelationsConfig);
75 32
    }
76
77 1
    public function addSecondaryRelationFields(array $fields)
78
    {
79 1
        foreach ($this->modelAbstractor->getRelations() as $relationKey => $relation) {
80
            /** @var RelationAbstractorContract $relation */
81 1
            foreach ($relation->getEditFields($relationKey) as $editGroupName => $editGroup) {
82
                $fields[$this->name][$editGroupName] = $editGroup;
83 1
            }
84 1
        }
85
86 1
        return $fields;
87
    }
88
89
    public function getName()
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * return null|string.
96
     */
97
    public function getDisplay()
98
    {
99
        if (!empty($this->config['display'])) {
100
            return $this->config['display'];
101
        }
102
    }
103
104 4 View Code Duplication
    public function getPresentation()
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...
105
    {
106 4
        if ($this->presentation) {
107
            return transcrud($this->presentation);
108
        }
109
110 4
        $nameWithSpaces = str_replace('_', ' ', $this->name);
111 4
        $namePieces = explode(' ', $nameWithSpaces);
112 4
        $namePieces = array_filter(array_map('trim', $namePieces));
113
114 4
        return transcrud(ucfirst(implode(' ', $namePieces)));
115
    }
116
117 3
    public function getType()
118
    {
119 3
        return get_class($this);
120
    }
121
122
    public function getModelAbstractor()
123
    {
124
        return $this->modelAbstractor;
125
    }
126
127
    /**
128
     * @return Collection
129
     */
130 4
    public function getSecondaryRelations()
131
    {
132 4
        return $this->modelAbstractor->getRelations();
133
    }
134
135
    /**
136
     * @param Model $relatedModel
137
     *
138
     * @return Relation
139
     */
140
    public function setRelatedModel(Model $relatedModel)
141
    {
142
        $this->relatedModel = $relatedModel;
143
144
        return $this;
145
    }
146
}
147