Template   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 6
Bugs 4 Features 2
Metric Value
eloc 75
c 6
b 4
f 2
dl 0
loc 136
rs 9.84
wmc 32

4 Methods

Rating   Name   Duplication   Size   Complexity  
C getPageViews() 0 41 14
A pages() 0 3 1
B updateFromRequest() 0 38 7
B getEmailTemplates() 0 30 10
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
    {
12
        return $this->hasMany('Chuckbe\Chuckcms\Models\Page');
13
    }
14
15
    protected $casts = [
16
        'fonts' => 'array',
17
        'css'   => 'array',
18
        'js'    => 'array',
19
        'json'  => 'array',
20
    ];
21
22
    /**
23
     * The attributes that are mass assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = [
28
        'name', 'slug', 'hintpath', 'path', 'type', 'version', 'author', 'active', 'fonts', 'css', 'js', 'json',
29
    ];
30
31
    public function getEmailTemplates()
32
    {
33
        $templates = $this->where('active', 1)->where('type', 'default')->get();
34
        $emailTemplates = [];
35
        foreach ($templates as $template) {
36
            if (file_exists(base_path('vendor/'.$template->path.'/src/views/templates/'.$template->slug.'/mails'))) {
37
                $mailDir = array_slice(scandir(base_path('vendor/'.$template->path.'/src/views/templates/'.$template->slug.'/mails')), 2);
38
                if (count($mailDir) > 0) {
39
                    $emailTemplates[$template->slug]['hintpath'] = $template->hintpath;
40
                    foreach ($mailDir as $mdKey => $mdValue) {
41
                        if (strpos($mdValue, '.blade.php')) {
42
                            $emailTemplates[$template->slug]['files'][] = str_replace('.blade.php', '', $mdValue);
43
                        }
44
                    }
45
                }
46
            }
47
            if (file_exists(base_path('resources/views/vendor/'.$template->slug.'/templates/'.$template->slug.'/mails'))) {
48
                $mailDirVendor = array_slice(scandir(base_path('resources/views/vendor/'.$template->slug.'/templates/'.$template->slug.'/mails')), 2);
49
                if (count($mailDirVendor) > 0) {
50
                    $emailTemplates[$template->slug]['hintpath'] = $template->hintpath;
51
                    foreach ($mailDirVendor as $mdKey => $mdValue) {
52
                        if (strpos($mdValue, '.blade.php')) {
53
                            $emailTemplates[$template->slug]['files'][] = str_replace('.blade.php', '', $mdValue);
54
                        }
55
                    }
56
                }
57
            }
58
        }
59
60
        return $emailTemplates;
61
    }
62
63
    public function getPageViews()
64
    {
65
        $templates = $this->where('active', 1)->get();
66
        $pageViews = [];
67
        foreach ($templates as $template) {
68
            if (file_exists(base_path('packages/'.$template->path.'/src/views/templates/'.$template->slug))) {
69
                $pageDir = array_slice(scandir(base_path('packages/'.$template->path.'/src/views/templates/'.$template->slug)), 2);
70
                if (count($pageDir) > 0) {
71
                    $pageViews[$template->slug]['hintpath'] = $template->hintpath;
72
                    foreach ($pageDir as $pdKey => $pdValue) {
73
                        if (strpos($pdValue, '.blade.php')) {
74
                            $pageViews[$template->slug]['files'][] = str_replace('.blade.php', '', $pdValue);
75
                        }
76
                    }
77
                }
78
            }
79
            if (file_exists(base_path('vendor/'.$template->path.'/src/views/templates/'.$template->slug))) {
80
                $pageDir = array_slice(scandir(base_path('vendor/'.$template->path.'/src/views/templates/'.$template->slug)), 2);
81
                if (count($pageDir) > 0) {
82
                    $pageViews[$template->slug]['hintpath'] = $template->hintpath;
83
                    foreach ($pageDir as $pdKey => $pdValue) {
84
                        if (strpos($pdValue, '.blade.php')) {
85
                            $pageViews[$template->slug]['files'][] = str_replace('.blade.php', '', $pdValue);
86
                        }
87
                    }
88
                }
89
            }
90
            if (file_exists(base_path('resources/views/vendor/'.$template->slug.'/templates/'.$template->slug))) {
91
                $pageDir = array_slice(scandir(base_path('resources/views/vendor/'.$template->slug.'/templates/'.$template->slug)), 2);
92
                if (count($pageDir) > 0) {
93
                    $pageViews[$template->slug]['hintpath'] = $template->hintpath;
94
                    foreach ($pageDir as $pdKey => $pdValue) {
95
                        if (strpos($pdValue, '.blade.php')) {
96
                            $pageViews[$template->slug]['files'][] = str_replace('.blade.php', '', $pdValue);
97
                        }
98
                    }
99
                }
100
            }
101
        }
102
103
        return $pageViews;
104
    }
105
106
    public function updateFromRequest(Request $request)
107
    {
108
        $template = $this->where('id', $request->template_id)->first();
109
110
        $template->name = $request->template_name;
111
112
        $fonts = [];
113
        $fonts['raw'] = $request->template_fonts;
114
        $template->fonts = $fonts;
115
116
        $css = [];
117
        $countCss = count($request->css_slug);
118
        for ($i = 0; $i < $countCss; $i++) {
119
            $css[$request->css_slug[$i]]['href'] = $request->css_href[$i];
120
            $css[$request->css_slug[$i]]['asset'] = $request->css_asset[$i] == 1 ? 'true' : 'false';
121
        }
122
123
        $template->css = $css;
124
125
        $js = [];
126
        $countJs = count($request->js_slug);
127
        for ($k = 0; $k < $countJs; $k++) {
128
            $js[$request->js_slug[$k]]['href'] = $request->js_href[$k];
129
            $js[$request->js_slug[$k]]['asset'] = $request->js_asset[$k] == 1 ? 'true' : 'false';
130
        }
131
132
        $template->js = $js;
133
134
        $json = $template->json;
135
        if (count($json) > 0) {
0 ignored issues
show
Bug introduced by
$json of type string is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

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

135
        if (count(/** @scrutinizer ignore-type */ $json) > 0) {
Loading history...
136
            foreach ($request->json_slug as $jsonKey => $jsonValue) {
137
                $json[$jsonKey]['value'] = $jsonValue;
138
            }
139
140
            $template->json = $json;
141
        }
142
143
        $template->update();
144
    }
145
}
146