Passed
Push — master ( f31008...6e362d )
by Karel
06:22
created

Template   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 24
eloc 61
dl 0
loc 113
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getPageViews() 0 29 10
A pages() 0 2 1
A updateFromRequest() 0 29 3
B getEmailTemplates() 0 29 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
        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
104
        for ($i=0; $i < count($request->css_slug); $i++) { 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
105
            $css[$request->css_slug[$i]]['href'] = $request->css_href[$i];
106
            $css[$request->css_slug[$i]]['asset'] = $request->css_asset[$i];
107
        }
108
109
        $template->css = $css;
110
111
        $js = [];
112
113
        for ($k=0; $k < count($request->js_slug); $k++) { 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
114
            $js[$request->js_slug[$k]]['href'] = $request->js_href[$k];
115
            $js[$request->js_slug[$k]]['asset'] = $request->js_asset[$k];
116
        }
117
118
        $template->js = $js;
119
120
        $template->update();
121
    }
122
}
123