Completed
Push — master ( 1ebb56...670045 )
by Adrian
05:52
created

Relation   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 68.28%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
c 10
b 0
f 0
dl 12
loc 119
ccs 28
cts 41
cp 0.6828
rs 10
wmc 13
lcom 2
cbo 6

9 Methods

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