Relation::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
        $config = array_merge($this->config, $relatedmodelRelationsConfig);
75
76 32
        $this->modelAbstractor = \App::make('Anavel\Crud\Contracts\Abstractor\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $config);
77 32
    }
78
79 1
    public function addSecondaryRelationFields(array $fields)
80
    {
81 1
        foreach ($this->modelAbstractor->getRelations() as $relationKey => $relation) {
82
            /** @var RelationAbstractorContract $relation */
83 1
            foreach ($relation->getEditFields($relationKey) as $editGroupName => $editGroup) {
84
                $fields[$this->name][$editGroupName] = $editGroup;
85 1
            }
86 1
        }
87
88 1
        return $fields;
89
    }
90
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * return null|string.
98
     */
99
    public function getDisplay()
100
    {
101
        if (!empty($this->config['display'])) {
102
            return $this->config['display'];
103
        }
104
    }
105
106 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...
107
    {
108 4
        if ($this->presentation) {
109
            return transcrud($this->presentation);
110
        }
111
112 4
        $nameWithSpaces = str_replace('_', ' ', $this->name);
113 4
        $namePieces = explode(' ', $nameWithSpaces);
114 4
        $namePieces = array_filter(array_map('trim', $namePieces));
115
116 4
        return transcrud(ucfirst(implode(' ', $namePieces)));
117
    }
118
119 3
    public function getType()
120
    {
121 3
        return get_class($this);
122
    }
123
124
    public function getModelAbstractor()
125
    {
126
        return $this->modelAbstractor;
127
    }
128
129
    /**
130
     * @return Collection
131
     */
132 4
    public function getSecondaryRelations()
133
    {
134 4
        return $this->modelAbstractor->getRelations();
135
    }
136
137
    /**
138
     * @param Model $relatedModel
139
     *
140
     * @return Relation
141
     */
142
    public function setRelatedModel(Model $relatedModel)
143
    {
144
        $this->relatedModel = $relatedModel;
145
146
        return $this;
147
    }
148
}
149