Passed
Pull Request — master (#18)
by Prateek
04:52
created

Controller::getFileUploads()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 16
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Backend;
3
4
use Prateekkarki\Laragen\Generators\BaseGenerator;
5
use Prateekkarki\Laragen\Generators\GeneratorInterface;
6
use Prateekkarki\Laragen\Models\DataOption;
7
8
class Controller extends BaseGenerator implements GeneratorInterface
9
{
10
    public function generate()
11
    {
12
        $controllerTemplate = $this->buildTemplate('backend/Controller', [
13
            '{{modelName}}'          => $this->module->getModelName(),
14
            '{{moduleName}}'         => $this->module->getModuleName(),
15
            '{{modelNameLowercase}}' => $this->module->getModelNameLowercase(),
16
            '{{fileUploads}}'        => $this->getFileUploads(),
17
            '{{foreignData}}'        => $this->getForeignData(),
18
            '{{usedModels}}'         => $this->getUsedModels(),
19
            '{{fileExtentions}}'     => $this->getFileExtentionData()
20
        ]);
21
        
22
        $fullFilePath = $this->getPath("app/Http/Controllers/Backend/").$this->module->getModelName()."Controller".".php";
23
        file_put_contents($fullFilePath, $controllerTemplate);
24
        return $fullFilePath;
25
    }
26
27
    protected function getFileUploads(){
28
        $fileUploads = "";
29
        $fileFields = $this->module->getFileColumns();
30
        if(empty($fileFields)) return "";
31
        if(count($fileFields)>1){
32
            $fileUploads .= $this->buildTemplate('backend/fragments/upload-process', [
33
                '{{modelNameLowercase}}' => $this->module->getModelNameLowercase(),
34
                '{{fileFields}}'         => implode('", "', $fileFields),
35
            ]);
36
        }else{
37
            $fileField = $fileFields[0];
38
            $fileUploads .= 'if ($request->has("'.$fileField.'")) {'.PHP_EOL;
39
            $fileUploads .= $this->getTabs(3).'$this->uploader->process($request->input("'.$fileField.'"), "category");'.PHP_EOL;
40
            $fileUploads .= $this->getTabs(2).'}'.PHP_EOL;
41
        }
42
        return $fileUploads;
43
    }
44
45
    protected function getForeignData(){
46
        $foreignData = "";
47
        $parents = $this->module->getForeignData();
48
        foreach($parents as $parent){
49
            $foreignData .= "'".$parent['parentModule']."' => ".$parent['parentModel']."::all()";
50
            $foreignData .= ($parent==last($parents)) ? '' : ', ';
51
        }
52
        return $foreignData;
53
    }
54
55
    protected function getUsedModels() {
56
        $foreignModels = $this->module->getForeignColumns();
57
        $namespace = "App\\Models\\";
58
        $usedModels = "use ".$namespace.$this->module->getModelName().";";
59
60
        foreach ($foreignModels as $models) {
61
            foreach ($models as $column => $module) {
62
                $namespace = ($module == 'users' && class_exists('App\\User')) ? "App\\" : "App\\Models\\";
63
                $class = $namespace.$this->moduleToModelName($module);
64
                $usedModels .= PHP_EOL."use ".$class.";";
65
            }
66
        }
67
        return $usedModels;
68
    }
69
70
    public function getFileExtentionData()
71
    {
72
        $controller_ = '';
73
        foreach($this->module->getFileColumns('file') as $column){
74
            $controller_ = "$".$this->module->getModelNameLowercase()."['".$column."_extention'] =".' getFileExtention($'.$this->module->getModelNameLowercase()."->".$column.");";
75
        }
76
        return $controller_;
77
    }
78
    
79
}
80