Passed
Pull Request — master (#23)
by Prateek
07:40
created

RelatedType   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 85
rs 10
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getPivotColumns() 0 13 2
A getPivotTable() 0 5 1
A getPivotSchema() 0 11 1
A getParentKey() 0 3 1
A getOptionModel() 0 3 1
A getMigrationPivot() 0 5 1
A getTypeColumns() 0 3 1
A getPivot() 0 5 1
A getChildKey() 0 3 1
A getChildModel() 0 3 1
A getRelatedModel() 0 3 1
1
<?php
2
namespace Prateekkarki\Laragen\Models\Types\Relational\Multiple;
3
use Prateekkarki\Laragen\Models\TypeResolver;
4
use Illuminate\Support\Str;
5
6
use Prateekkarki\Laragen\Models\Types\Relational\MultipleType;
7
8
class RelatedType extends MultipleType
9
{
10
    protected $hasPivot = true;
11
	protected $formType = 'related';
12
    protected $stubs = [
13
        'foreignMethod' => 'common/Models/fragments/belongsToMany'
14
    ];
15
16
    public function getPivotSchema()
17
    {
18
        $moduleName = $this->getParentModule();
19
        $schema = PHP_EOL.$this->getTabs(3);
20
        $schema .= '$table->bigInteger("'.$this->getParentKey().'")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3);
21
        $schema .= '$table->foreign("'.$this->getParentKey().'")->references("id")->on("'.$moduleName.'")->onDelete("set null");'.PHP_EOL.$this->getTabs(3);
22
23
        $schema .= '$table->bigInteger("'. $this->getChildKey() .'")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3);
24
        $schema .= '$table->foreign("'.$this->getChildKey().'")->references("id")->on("'.$this->typeOption.'")->onDelete("set null");'.PHP_EOL;
25
26
        return $schema;
27
    }
28
29
    public function getParentKey()
30
    {
31
        return $this->getParentModelLowercase()."_id";
32
    }
33
    
34
    public function getRelatedModel()
35
    {
36
        return $this->getOptionModel();
37
    }
38
39
    public function getOptionModel()
40
    {
41
        return ucfirst(Str::camel(Str::singular($this->typeOption)));
42
    }
43
44
    public function getChildKey()
45
    {
46
        return Str::singular($this->columnName) ."_id";
47
    }
48
49
    public function getChildModel()
50
    {
51
        return ucfirst(Str::camel(Str::singular($this->columnName)));
52
    }
53
54
    public function getPivotTable()
55
    {
56
        $modelArray = [$this->getParentModelLowercase(), strtolower(Str::singular($this->columnName))];
57
        sort($modelArray);
58
        return implode("_", $modelArray);
59
    }
60
61
    public function getMigrationPivot()
62
    {
63
        $modelArray = [$this->getParentModel(), $this->getChildModel()];
64
        sort($modelArray);
65
        return implode("", $modelArray);
66
    }
67
68
    public function getPivot()
69
    {
70
        $modelArray = [$this->getParentModel(), $this->getChildModel()];
71
        sort($modelArray);
72
        return implode("", $modelArray);
73
    }
74
75
    public function getPivotColumns()
76
    {
77
        $columnModels = [];
78
        $columns = [
79
            $this->getParentModelLowercase() => 'parent:'.$this->getParentModule(), 
80
            Str::singular($this->columnName) => 'parent:'.$this->typeOption, 
81
        ];
82
        
83
        foreach ($columns as $column => $optionString) {
84
            $data = new TypeResolver($this->getPivotTable(), $column, $optionString);
85
            $columnModels[$column] = $data->getLaragenType();
86
        }
87
        return $columnModels;
88
    }
89
90
    public function getTypeColumns()
91
    {
92
        return [$this->getParentModelLowercase().'_id', strtolower(Str::singular($this->typeOption)) . '_id'];
93
    }
94
}
95