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

MultipleType::getPivotColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
namespace Prateekkarki\Laragen\Models\Types\File;
3
use Prateekkarki\Laragen\Models\TypeResolver;
4
use Prateekkarki\Laragen\Models\Types\FileType;
5
use Illuminate\Support\Str;
6
7
class MultipleType extends FileType
8
{
9
    protected $hasModel = true;
10
    protected $relationalType = true;
11
    protected $hasMultipleFiles = true;
12
    protected $formType = 'multipleFiles';
13
    protected $stubs = [
14
        'modelMethod' => 'common/Models/fragments/belongsTo',
15
        'foreignMethod' => 'common/Models/fragments/hasMany'
16
    ];
17
    
18
    public function getPivotSchema()
19
    {
20
        $modelName = $this->getParentModelLowercase();
21
        $moduleName = $this->getParentModule();
22
        $schema = '$table->bigInteger("'.$modelName.'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3);
23
        $schema .= "\$table->foreign('".$modelName."_id')->references('id')->on('".$moduleName."')->onDelete('set null');".PHP_EOL.$this->getTabs(3);
24
        $schema .= '$table->string("filename", 192);'.PHP_EOL.$this->getTabs(3);
25
        $schema .= '$table->integer("size");'.PHP_EOL.$this->getTabs(3);
26
        $schema .= '$table->timestamps();'.PHP_EOL.$this->getTabs(3);
27
        return $schema;
28
    }
29
30
    public function getPivotTable()
31
    {
32
        return $this->getParentModelLowercase() . "_" . strtolower(Str::plural($this->columnName));
33
    }
34
35
    public function getMigrationPivot()
36
    {
37
        return $this->getParentModel() . Str::plural($this->getChildModel());
38
    }
39
40
    public function getRelatedModel()
41
    {
42
        return $this->getPivot();
43
    }
44
45
    public function getPivot()
46
    {
47
        return $this->getParentModel() . $this->getChildModel();
48
    }
49
    
50
    public function getPivotColumns()
51
    {
52
        $columnModels = [];
53
        foreach ($this->getLaragenColumns() as $column => $optionString) {
0 ignored issues
show
Bug introduced by
The method getLaragenColumns() does not exist on Prateekkarki\Laragen\Mod...Types\File\MultipleType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        foreach ($this->/** @scrutinizer ignore-call */ getLaragenColumns() as $column => $optionString) {
Loading history...
54
            $data = new TypeResolver($this->getPivotTable(), $column, $optionString);
55
            $columnModels[$column] = $data->getLaragenType();
56
        }
57
        return $columnModels;
58
    }
59
60
    public function getTypeColumns()
61
    {
62
        return [$this->getParentModelLowercase().'_id', 'filename', 'size'];
63
    }
64
}
65