Passed
Push — master ( 9bd6cb...5aafeb )
by Karel
07:55
created

Template   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 67
c 4
b 2
f 0
dl 0
loc 120
rs 10
wmc 27

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pages() 0 2 1
B getPageViews() 0 29 10
B getEmailTemplates() 0 29 10
B updateFromRequest() 0 36 6
1
<?php
2
3
namespace Chuckbe\Chuckcms\Models;
4
5
use Eloquent;
6
use Illuminate\Http\Request;
7
8
class Template extends Eloquent
9
{
10
    public function pages(){
11
        return $this->hasMany('Chuckbe\Chuckcms\Models\Page');
12
    }
13
14
    protected $casts = [
15
        'fonts' => 'array',
16
        'css' => 'array',
17
        'js' => 'array',
18
        'json' => 'array'
19
    ];
20
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'name', 'slug', 'hintpath', 'path', 'type', 'version', 'author', 'active', 'fonts', 'css', 'js', 'json'
28
    ];
29
30
    public function getEmailTemplates()
31
    {
32
        $templates = $this->where('active', 1)->where('type', 'default')->get();
33
        $emailTemplates = [];
34
        foreach ($templates as $template) {
35
            if (file_exists(base_path('vendor/' . $template->path . '/src/views/templates/' . $template->slug . '/mails'))) {
36
                $mailDir = array_slice(scandir(base_path('vendor/' . $template->path . '/src/views/templates/' . $template->slug . '/mails')), 2);
0 ignored issues
show
Bug introduced by
It seems like scandir(base_path('vendo...late->slug . '/mails')) can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

36
                $mailDir = array_slice(/** @scrutinizer ignore-type */ scandir(base_path('vendor/' . $template->path . '/src/views/templates/' . $template->slug . '/mails')), 2);
Loading history...
37
                if (count($mailDir) > 0) {
38
                    $emailTemplates[$template->slug]['hintpath'] = $template->hintpath;
39
                    foreach ($mailDir as $mdKey => $mdValue) {
40
                        if (strpos($mdValue, '.blade.php')) {
41
                            $emailTemplates[$template->slug]['files'][] = str_replace('.blade.php', '', $mdValue);
42
                        }    
43
                    }
44
                }
45
            }
46
            if (file_exists(base_path('resources/views/vendor/' . $template->slug . '/templates/' . $template->slug . '/mails'))) {
47
                $mailDirVendor = array_slice(scandir(base_path('resources/views/vendor/' . $template->slug . '/templates/' . $template->slug . '/mails')), 2);
48
                if (count($mailDirVendor) > 0) {
49
                    $emailTemplates[$template->slug]['hintpath'] = $template->hintpath;
50
                    foreach ($mailDirVendor as $mdKey => $mdValue) {
51
                        if (strpos($mdValue, '.blade.php')) {
52
                            $emailTemplates[$template->slug]['files'][] = str_replace('.blade.php', '', $mdValue);
53
                        }    
54
                    }
55
                }
56
            }
57
        }
58
        return $emailTemplates;
59
    }
60
61
    public function getPageViews()
62
    {
63
        $templates = $this->where('active', 1)->where('type', 'default')->get();
64
        $pageViews = [];
65
        foreach ($templates as $template) {
66
            if (file_exists(base_path('vendor/' . $template->path . '/src/views/templates/' . $template->slug))) {
67
                $pageDir = array_slice(scandir(base_path('vendor/' . $template->path . '/src/views/templates/' . $template->slug)), 2);
0 ignored issues
show
Bug introduced by
It seems like scandir(base_path('vendo...s/' . $template->slug)) can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

67
                $pageDir = array_slice(/** @scrutinizer ignore-type */ scandir(base_path('vendor/' . $template->path . '/src/views/templates/' . $template->slug)), 2);
Loading history...
68
                if (count($pageDir) > 0) {
69
                    $pageViews[$template->slug]['hintpath'] = $template->hintpath;
70
                    foreach ($pageDir as $pdKey => $pdValue) {
71
                        if (strpos($pdValue, '.blade.php')) {
72
                            $pageViews[$template->slug]['files'][] = str_replace('.blade.php', '', $pdValue);
73
                        }    
74
                    }
75
                }
76
            }
77
            if (file_exists(base_path('resources/views/vendor/' . $template->slug . '/templates/' . $template->slug))) {
78
                $pageDir = array_slice(scandir(base_path('resources/views/vendor/' . $template->slug . '/templates/' . $template->slug)), 2);
79
                if (count($pageDir) > 0) {
80
                    $pageViews[$template->slug]['hintpath'] = $template->hintpath;
81
                    foreach ($pageDir as $pdKey => $pdValue) {
82
                        if (strpos($pdValue, '.blade.php')) {
83
                            $pageViews[$template->slug]['files'][] = str_replace('.blade.php', '', $pdValue);
84
                        }    
85
                    }
86
                }
87
            }
88
        }
89
        return $pageViews;
90
    }
91
92
    public function updateFromRequest(Request $request)
93
    {
94
        $template = $this->where('id', $request->template_id)->first();
95
96
        $template->name = $request->template_name;
97
98
        $fonts = [];
99
        $fonts['raw'] = $request->template_fonts;
100
        $template->fonts = $fonts;
101
102
        $css = [];
103
        $countCss = count($request->css_slug);
104
        for ($i=0; $i < $countCss; $i++) { 
105
            $css[$request->css_slug[$i]]['href'] = $request->css_href[$i];
106
            $css[$request->css_slug[$i]]['asset'] = $request->css_asset[$i] == 1 ? 'true' : 'false';
107
        }
108
109
        $template->css = $css;
110
111
        $js = [];
112
        $countJs = count($request->js_slug);
113
        for ($k=0; $k < $countJs; $k++) { 
114
            $js[$request->js_slug[$k]]['href'] = $request->js_href[$k];
115
            $js[$request->js_slug[$k]]['asset'] = $request->js_asset[$k] == 1 ? 'true' : 'false';
116
        }
117
118
        $template->js = $js;
119
120
        $json = $template->json;
121
        foreach ($request->json_slug as $jsonKey => $jsonValue) { 
122
            $json[$jsonKey]['value'] = $jsonValue;
123
        }
124
125
        $template->json = $json;
126
127
        $template->update();
128
    }
129
}
130