Completed
Push — master ( e1cc2a...6b3a6a )
by
unknown
03:17
created

Relation::getDisplay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
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
    public function getPresentation()
93
    {
94 4
        return $this->presentation ? : ucfirst(str_replace('_', ' ', $this->name));
95
    }
96
97 3
    public function getType()
98
    {
99 3
        return get_class($this);
100
    }
101
102
    public function getModelAbstractor()
103
    {
104
        return $this->modelAbstractor;
105
    }
106
107
    /**
108
     * @return Collection
109
     */
110 4
    public function getSecondaryRelations()
111
    {
112 4
        return $this->modelAbstractor->getRelations();
113
    }
114
115
    /**
116
     * @param Model $relatedModel
117
     * @return Relation
118
     */
119
    public function setRelatedModel(Model $relatedModel)
120
    {
121
        $this->relatedModel = $relatedModel;
122
123
        return $this;
124
    }
125
}
126