Model::getForeignMethods()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 16
rs 9.8666
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Common;
3
4
use Prateekkarki\Laragen\Generators\BaseGenerator;
5
use Prateekkarki\Laragen\Generators\GeneratorInterface;
6
7
class Model extends BaseGenerator implements GeneratorInterface
8
{
9
    protected $destination = "laragen/app/Models";
10
    protected $namespace  = "Laragen\App\Models";
11
    protected $template = "common/Models/Model";
12
    protected $pivotTemplate = "common/Models/Pivot";
13
14
    protected $childDestination = "app/Models";
15
    protected $childNamespace  = "App\Models";
16
17
    public function generate()
18
    {
19
        $generatedFiles = [];
20
        $modelTemplate = $this->buildTemplate($this->template, [
21
            '{{namespace}}'     => $this->namespace,
22
            '{{modelName}}'       => $this->module->getModelName(),
23
            '{{massAssignables}}' => implode("', '", $this->module->getColumns(true, true)),
24
            '{{usedModels}}'      => $this->getUsedModels(),
25
            '{{foreignMethods}}'  => $this->getForeignMethods()
26
        ]);
27
28
        $generatedFiles[] = $this->generateFile($modelTemplate);
29
30
        foreach ($this->module->getFilteredColumns('hasPivot') as $type) {
31
            $typeTemplate = $this->buildTemplate($this->pivotTemplate, [
32
                '{{namespace}}'     => $this->namespace,
33
                '{{pivotName}}'       => $type->getPivot(),
34
                '{{massAssignables}}' => implode("', '", $type->getTypeColumns()),
35
                '{{foreignMethods}}'  => $this->getTypeForeignMethods($type),
36
            ]);
37
38
            $generatedFiles[] = $this->generateFile($typeTemplate, $type->getPivot());
39
        }
40
41
        foreach ($this->module->getFilteredColumns(['hasModel', 'hasOptions']) as $type) {
42
            $pivotModel = $type->getPivot();
43
            $typeTemplate = $this->buildTemplate($this->template, [
44
                '{{namespace}}'     => $this->namespace,
45
                '{{modelName}}'       => $pivotModel,
46
                '{{massAssignables}}' => implode("', '", $type->getTypeColumns()),
47
                '{{usedModels}}'      => $this->getUsedModels($pivotModel),
48
                '{{foreignMethods}}'  => $this->getTypeForeignMethods($type),
49
            ]);
50
51
            $generatedFiles[] = $this->generateFile($typeTemplate, $pivotModel);
52
        }
53
54
        return $generatedFiles;
55
    }
56
57
    protected function getTypeForeignMethods($type)
58
    {
59
        $foreignMethods = "";
60
        $stub = $type->getStub('modelMethod') ?: 'common/Models/fragments/belongsTo';
61
        $foreignMethods .= $this->buildTemplate($stub, [
62
            '{{columnName}}'  => $type->getColumn(),
63
            '{{parent}}'      => $type->getParentModelLowercase(),
64
            '{{parentModel}}' => $type->getParentModel(),
65
            '{{relatedModel}}' => $type->getRelatedModel(),
66
        ]);
67
68
        return $foreignMethods;
69
    }
70
71
    protected function getUsedModels($pivotModel = false) {
72
        $usedModels = "";
73
        $classes = [];
74
        foreach ($this->module->getFilteredColumns(['hasSingleRelation', 'hasPivot', 'hasModel']) as $type) {
75
            $model = $type->getRelatedModel();
76
            $class = ($model == 'User') ? config('laragen.options.user_model') : "App\\Models\\".$model;
77
            if (in_array($class, $classes) || $model == $this->module->getModelName() || $model == $pivotModel) {
78
                continue;
79
            }
80
            $classes[] = $class;
81
            $usedModels .= PHP_EOL."use ".$class.";";
82
        }
83
        return $usedModels;
84
    }
85
86
    protected function getForeignMethods()
87
    {
88
        $foreignMethods = "";
89
        foreach ($this->module->getFilteredColumns(['hasPivot', 'hasSingleRelation', 'hasModel']) as $type) {
90
            $stub = $type->getStub('foreignMethod') ?: 'common/Models/fragments/hasOne';
91
            $foreignMethods .= $this->buildTemplate($stub, [
92
                '{{columnName}}'   => $type->getColumn(),
93
                '{{parent}}'       => $type->getParentModelLowercase(),
94
                '{{relatedModel}}' => $type->getRelatedModel(),
95
                '{{table}}'        => $type->getPivotTable(),
96
                '{{parentModel}}'  => $type->getParentModel(),
97
                '{{parentId}}'     => $type->getParentModelLowercase()."_id",
98
                '{{childId}}'      => $type->getChildKey(),
99
            ]);
100
        }
101
        return $foreignMethods;
102
    }
103
}
104