Passed
Push — master ( fd21f2...66fc67 )
by Karel
08:29
created

PageRepository::updatePage()   B

Complexity

Conditions 8
Paths 26

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 37
c 1
b 0
f 1
nc 26
nop 1
dl 0
loc 54
rs 8.0835

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck;
4
5
use Chuckbe\Chuckcms\Models\Page;
6
use Illuminate\Http\Request;
7
use ChuckSite;
0 ignored issues
show
Bug introduced by
The type ChuckSite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class PageRepository
10
{
11
	protected $page;
12
13
    public function __construct(Page $page)
14
    {
15
        $this->page = $page;
16
    }
17
18
    public function find(string $id)
19
    {
20
    	return $this->page->where('id', $id)->first();
21
    }
22
23
    public function create(Request $request)
24
    {
25
    	$page = new Page();
26
27
        $meta = [];
28
        foreach (ChuckSite::getSupportedLocales() as $langKey => $langValue) {
29
            $page->setTranslation('title', $langKey, $request->get('page_title')[$langKey]);
30
            $page->setTranslation('slug', $langKey, $request->get('page_slug')[$langKey]);
31
32
            $meta[$langKey]['title'] = $request->get('meta_title')[$langKey];
33
            $meta[$langKey]['description'] = $request->get('meta_description')[$langKey];
34
            $meta[$langKey]['keywords'] = $request->get('meta_keywords')[$langKey];
35
            
36
            $meta[$langKey]['og:url'] = $request->get('page_slug')[$langKey];
37
            $meta[$langKey]['og:type'] = 'website';
38
            $meta[$langKey]['og:title'] = $request->get('meta_title')[$langKey];
39
            $meta[$langKey]['og:description'] = $request->get('meta_description')[$langKey];
40
            $meta[$langKey]['og:site_name'] = $request->get('meta_title')[$langKey];
41
            $meta[$langKey]['og:image'] = $request->get('meta_image');
42
            
43
            if ($request->get('meta_robots_index')[$langKey] == '1') {
44
                $index = 'index, ';
45
            } else {
46
                $index = 'noindex, ';
47
            }
48
49
            if ($request->get('meta_robots_follow')[$langKey] == '1') {
50
                $follow = 'follow';
51
            } else {
52
                $follow = 'nofollow';
53
            }
54
55
            $meta[$langKey]['robots'] = $index.$follow;
56
            $meta[$langKey]['googlebots'] = $index.$follow;
57
            
58
            $count = count($request->get('meta_key')[$langKey]);
59
            for ($i = 0; $i < $count; $i++) {
60
                $meta[$langKey][$request->get('meta_key')[$langKey][$i]] = $request->get('meta_value')[$langKey][$i];
61
            }
62
        }
63
        $page->meta = $meta;
64
65
        $page->template_id = $request['template_id'];
66
        $page->page = $request['page'];
67
        $page->active = $request['active'];
68
        $page->isHp = $request['isHp'];
69
        $page->order = ($this->page->max('order') ?? 0) + 1;
70
71
        $page->css = $request->get('css');
72
        $page->js = $request->get('js');
73
74
        $page->save();
75
    }
76
77
    public function updatePage($request)
78
    {
79
        $page = $this->find($request['page_id']);
80
81
        $meta = [];
82
        foreach (ChuckSite::getSupportedLocales() as $langKey => $langValue) {
83
            $page->setTranslation('title', $langKey, $request->get('page_title')[$langKey]);
84
            $page->setTranslation('slug', $langKey, $request->get('page_slug')[$langKey]);
85
86
            $meta[$langKey]['title'] = $request->get('meta_title')[$langKey];
87
            $meta[$langKey]['description'] = $request->get('meta_description')[$langKey];
88
            $meta[$langKey]['keywords'] = $request->get('meta_keywords')[$langKey];
89
            
90
            $meta[$langKey]['og:url'] = $request->get('page_slug')[$langKey];
91
            $meta[$langKey]['og:type'] = 'website';
92
            $meta[$langKey]['og:title'] = $request->get('meta_title')[$langKey];
93
            $meta[$langKey]['og:description'] = $request->get('meta_description')[$langKey];
94
            $meta[$langKey]['og:site_name'] = $request->get('meta_title')[$langKey];
95
            $meta[$langKey]['og:image'] = $request->get('meta_image');
96
            
97
            if ($request->get('meta_robots_index')[$langKey] == '1') {
98
                $index = 'index, ';
99
            } else {
100
                $index = 'noindex, ';
101
            }
102
103
            if ($request->get('meta_robots_follow')[$langKey] == '1') {
104
                $follow = 'follow';
105
            } else {
106
                $follow = 'nofollow';
107
            }
108
109
            $meta[$langKey]['robots'] = $index.$follow;
110
            $meta[$langKey]['googlebots'] = $index.$follow;
111
112
            $count = count($request->get('meta_key')[$langKey]);
113
            for ($i = 0; $i < $count; $i++) {
114
            	if (!is_null($request->get('meta_value')[$langKey][$i])) {
115
            		$meta[$langKey][$request->get('meta_key')[$langKey][$i]] = $request->get('meta_value')[$langKey][$i];
116
            	}
117
            }
118
        }
119
        $page->meta = $meta;
120
121
        $page->template_id = $request['template_id'];
122
        $page->page = $request['page'];
123
        $page->active = $request['active'];
124
        $page->isHp = $request['isHp'];
125
        $page->roles = count(is_array($request['roles']) ? $request['roles'] : []) > 0 ? implode('|', $request['roles']) : null;
126
127
        $page->css = $request->get('css');
128
        $page->js = $request->get('js');
129
130
        $page->save();
131
    }
132
}
133