Completed
Push — master ( 08f3b7...ac4669 )
by Adrian
05:56
created

Relation::getModelAbstractor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 4
    public function getPresentation()
82
    {
83 4
        return $this->presentation ? : ucfirst(str_replace('_', ' ', $this->name));
84
    }
85
86 3
    public function getType()
87
    {
88 3
        return get_class($this);
89
    }
90
91
    public function getModelAbstractor()
92
    {
93
        return $this->modelAbstractor;
94 4
    }
95
96 4
    /**
97
     * @return Collection
98
     */
99
    public function getSecondaryRelations()
100
    {
101
        return $this->modelAbstractor->getRelations();
102
    }
103
104
    /**
105
     * @param Model $relatedModel
106
     * @return Relation
107
     */
108
    public function setRelatedModel(Model $relatedModel)
109
    {
110
        $this->relatedModel = $relatedModel;
111
112
        return $this;
113
    }
114
}
115