Code Duplication    Length = 149-149 lines in 2 locations

src/Scaffolder/Compilers/Blade/IndexViewCompiler.php 1 location

@@ 11-159 (lines=149) @@
8
use Scaffolder\Compilers\Support\PathParser;
9
use Scaffolder\Support\Directory;
10
11
class IndexViewCompiler extends AbstractViewCompiler
12
{
13
    /**
14
     * Compiles the index view.
15
     *
16
     * @param      $stub
17
     * @param      $modelName
18
     * @param      $modelData
19
     * @param      $scaffolderConfig
20
     * @param      $hash
21
     * @param null $extra
22
     *
23
     * @return string
24
     */
25
    public function compile($stub, $modelName, $modelData, $scaffolderConfig, $hash, $extra = null)
26
    {
27
        if (File::exists(base_path('scaffolder-config/cache/view_index_' . $hash . self::CACHE_EXT)))
28
        {
29
            return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
30
        }
31
        else
32
        {
33
            $this->stub = $stub;
34
35
            return $this->replaceClassName($modelName)
36
                ->replaceBreadcrumb($modelName)
37
                ->addDatatableFields($modelName, $modelData)
38
                ->setTableHeaders($modelData)
39
                ->replaceRoutePrefix($scaffolderConfig->generator->routing->prefix)
40
                ->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
41
        }
42
    }
43
44
    /**
45
     * Store the compiled stub.
46
     *
47
     * @param               $modelName
48
     * @param               $scaffolderConfig
49
     * @param               $compiled
50
     * @param FileToCompile $fileToCompile
51
     *
52
     * @return string
53
     */
54
    protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
55
    {
56
        $folder = PathParser::parse($scaffolderConfig->generator->paths->views) . strtolower($modelName) ;
57
        
58
        // create folder directory
59
        Directory::createIfNotExists($folder, 0755, true);
60
61
        $path = $folder  . '/index.blade.php';
62
63
        // Store in cache
64
        if ($fileToCompile->cached)
65
        {
66
            File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
67
        }
68
        else
69
        {
70
            File::put(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
71
            File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
72
        }
73
74
        return $path;
75
    }
76
77
    /**
78
     * Datatable fields.
79
     *
80
     * @param $modelName
81
     * @param $modelData
82
     *
83
     * @return $this
84
     */
85
    private function addDatatableFields($modelName, $modelData)
86
    {
87
        $fields = '';
88
        $firstIteration = true;
89
90
        foreach ($modelData->fields as $field)
91
        {
92
            if ($field->hideInListings == false)
93
            {
94
                if ($firstIteration)
95
                {
96
                    $fields .= sprintf("{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
97
                    $firstIteration = false;
98
                }
99
                else
100
                {
101
                    $fields .= sprintf("\t\t\t\t{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
102
                }
103
            }
104
        }
105
106
        $this->stub = str_replace('{{datatable_fields}}', $fields, $this->stub);
107
        $this->stub = str_replace('{{datatable_url}}', ucfirst($modelName), $this->stub);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Set index table headers.
114
     *
115
     * @param $modelData
116
     *
117
     * @return $this
118
     */
119
    private function setTableHeaders($modelData)
120
    {
121
        $fields = '';
122
        $firstIteration = true;
123
124
        foreach ($modelData->fields as $field)
125
        {
126
            if ($field->hideInListings == false)
127
            {
128
                if ($firstIteration)
129
                {
130
                    $fields .= sprintf("<th>%s</th>" . PHP_EOL, ucfirst($field->name));
131
                    $firstIteration = false;
132
133
                }
134
                else
135
                {
136
                    $fields .= sprintf("\t\t\t<th>%s</th>" . PHP_EOL, ucfirst($field->name));
137
                }
138
            }
139
        }
140
141
        $this->stub = str_replace('{{table_headers}}', $fields, $this->stub);
142
143
        return $this;
144
    }
145
146
    /**
147
     * Replace the prefix.
148
     *
149
     * @param $prefix
150
     *
151
     * @return $this
152
     */
153
    private function replaceRoutePrefix($prefix)
154
    {
155
        $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
156
157
        return $this;
158
    }
159
}

src/Scaffolder/Compilers/View/IndexViewCompiler.php 1 location

@@ 11-159 (lines=149) @@
8
use Scaffolder\Compilers\Support\PathParser;
9
use Scaffolder\Support\Directory;
10
11
class IndexViewCompiler extends AbstractViewCompiler
12
{
13
    /**
14
     * Compiles the index view.
15
     *
16
     * @param      $stub
17
     * @param      $modelName
18
     * @param      $modelData
19
     * @param      $scaffolderConfig
20
     * @param      $hash
21
     * @param null $extra
22
     *
23
     * @return string
24
     */
25
    public function compile($stub, $modelName, $modelData, $scaffolderConfig, $hash, $extra = null)
26
    {
27
        if (File::exists(base_path('scaffolder-config/cache/view_index_' . $hash . self::CACHE_EXT)))
28
        {
29
            return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
30
        }
31
        else
32
        {
33
            $this->stub = $stub;
34
35
            return $this->replaceClassName($modelName)
36
                ->replaceBreadcrumb($modelName)
37
                ->addDatatableFields($modelName, $modelData)
38
                ->setTableHeaders($modelData)
39
                ->replaceRoutePrefix($scaffolderConfig->routing->prefix)
40
                ->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
41
        }
42
    }
43
44
    /**
45
     * Store the compiled stub.
46
     *
47
     * @param               $modelName
48
     * @param               $scaffolderConfig
49
     * @param               $compiled
50
     * @param FileToCompile $fileToCompile
51
     *
52
     * @return string
53
     */
54
    protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
55
    {
56
        $folder = PathParser::parse($scaffolderConfig->paths->views) . strtolower($modelName) ;
57
        
58
        // create folder directory
59
        Directory::createIfNotExists($folder, 0755, true);
60
61
        $path = $folder  . '/index.blade.php';
62
63
        // Store in cache
64
        if ($fileToCompile->cached)
65
        {
66
            File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
67
        }
68
        else
69
        {
70
            File::put(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
71
            File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
72
        }
73
74
        return $path;
75
    }
76
77
    /**
78
     * Datatable fields.
79
     *
80
     * @param $modelName
81
     * @param $modelData
82
     *
83
     * @return $this
84
     */
85
    private function addDatatableFields($modelName, $modelData)
86
    {
87
        $fields = '';
88
        $firstIteration = true;
89
90
        foreach ($modelData->fields as $field)
91
        {
92
            if ($field->hideInListings == false)
93
            {
94
                if ($firstIteration)
95
                {
96
                    $fields .= sprintf("{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
97
                    $firstIteration = false;
98
                }
99
                else
100
                {
101
                    $fields .= sprintf("\t\t\t\t{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
102
                }
103
            }
104
        }
105
106
        $this->stub = str_replace('{{datatable_fields}}', $fields, $this->stub);
107
        $this->stub = str_replace('{{datatable_url}}', ucfirst($modelName), $this->stub);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Set index table headers.
114
     *
115
     * @param $modelData
116
     *
117
     * @return $this
118
     */
119
    private function setTableHeaders($modelData)
120
    {
121
        $fields = '';
122
        $firstIteration = true;
123
124
        foreach ($modelData->fields as $field)
125
        {
126
            if ($field->hideInListings == false)
127
            {
128
                if ($firstIteration)
129
                {
130
                    $fields .= sprintf("<th>%s</th>" . PHP_EOL, ucfirst($field->name));
131
                    $firstIteration = false;
132
133
                }
134
                else
135
                {
136
                    $fields .= sprintf("\t\t\t<th>%s</th>" . PHP_EOL, ucfirst($field->name));
137
                }
138
            }
139
        }
140
141
        $this->stub = str_replace('{{table_headers}}', $fields, $this->stub);
142
143
        return $this;
144
    }
145
146
    /**
147
     * Replace the prefix.
148
     *
149
     * @param $prefix
150
     *
151
     * @return $this
152
     */
153
    private function replaceRoutePrefix($prefix)
154
    {
155
        $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
156
157
        return $this;
158
    }
159
}