Completed
Push — master ( ed6e58...b3a5f7 )
by Davide
10:21
created

BlogController::saveOnDb()   F

Complexity

Conditions 14
Paths 8192

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 14

Importance

Changes 0
Metric Value
cc 14
eloc 22
nc 8192
nop 2
dl 0
loc 30
ccs 23
cts 23
cp 1
crap 14
rs 2.1
c 0
b 0
f 0

How to fix   Complexity   

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 DavideCasiraghi\LaravelSmartBlog\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use DavideCasiraghi\LaravelSmartBlog\Models\Blog;
8
use DavideCasiraghi\LaravelSmartBlog\Models\Post;
9
use DavideCasiraghi\LaravelSmartBlog\Models\Category;
10
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
11
12
class BlogController extends Controller
13
{
14
    /* Restrict the access to this resource just to logged in users */
15 11
    public function __construct()
16
    {
17 11
        $this->middleware('admin');
18 11
    }
19
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return \Illuminate\Http\Response
24
     */
25 5
    public function index()
26
    {
27 5
        $categories = Category::get();
28 5
        $blogs = Blog::latest()->paginate(10);
29
30
        // Countries available for translations
31 5
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
32
33 5
        return view('laravel-smart-blog::blogs.index', compact('blogs'))
34 5
            ->with('i', (request()->input('page', 1) - 1) * 10)
35 5
            ->with('categories', $categories)
36 5
            ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
37
    }
38
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44 1
    public function create()
45
    {
46
        //$categories = Category::get();
47 1
        $categories = Category::listsTranslations('name')->pluck('name', 'id');
48
49 1
        return view('laravel-smart-blog::blogs.create')
50 1
            ->with('categories', $categories);
51
    }
52
53
    /**
54
     * Store a newly created resource in storage.
55
     *
56
     * @param  \Illuminate\Http\Request  $request
57
     * @return \Illuminate\Http\Response
58
     */
59 2
    public function store(Request $request)
60
    {
61
        // Validate form datas
62 2
        $validator = Validator::make($request->all(), [
63 2
                'category_id' => 'required',
64
                'layout' => 'required',
65
            ]);
66 2
        if ($validator->fails()) {
67 1
            return back()->withErrors($validator)->withInput();
68
        }
69
70 1
        $blog = new Blog();
71
72 1
        $this->saveOnDb($request, $blog);
73
74 1
        return redirect()->route('blogs.index')
75 1
                        ->with('success', __('messages.blog_added_successfully'));
76
    }
77
78
    /**
79
     * Display the specified resource.
80
     *
81
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
82
     * @return \Illuminate\Http\Response
83
     */
84 1
    public function show(Blog $blog)
85
    {
86
        //$category = Category::where('id', $blog->category_id)->get();
87
88 1
        $category = Category::select('categories.id AS id', 'category_translations.name AS name', 'category_translations.description AS description', 'category_translations.slug AS slug', 'locale')
89 1
                ->join('category_translations', 'categories.id', '=', 'category_translations.category_id')->first();
90
91 1
        $posts = Post::where('category_id', $blog->category_id)->get();
92
93
        /*$posts = Post::select('post.id AS id', 'post_translations.title AS title', 'post_translations.body AS body', 'post_translations.slug AS slug', 'category_id', 'locale')
94
                ->join('post_translations', 'posts.id', '=', 'post_translations.post_id');
95
        */
96
97 1
        $columnsBootstrapClass = (! empty($blog->columns_number)) ? 'col-md-'.strval((12 / intval($blog->columns_number))) : '';
98
99
        //::where('code', 'gr')
100
101 1
        return view('laravel-smart-blog::blogs.show', compact('blog'))
102 1
                    ->with('category', $category)
103 1
                    ->with('posts', $posts)
104 1
                    ->with('columnsBootstrapClass', $columnsBootstrapClass);
105
    }
106
107
    /**
108
     * Show the form for editing the specified resource.
109
     *
110
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
111
     * @return \Illuminate\Http\Response
112
     */
113 1
    public function edit(Blog $blog)
114
    {
115 1
        $categories = Category::listsTranslations('name')->pluck('name', 'id');
116
117 1
        return view('laravel-smart-blog::blogs.edit', compact('blog'))
118 1
                    ->with('categories', $categories);
119
    }
120
121
    /**
122
     * Update the specified resource in storage.
123
     *
124
     * @param  \Illuminate\Http\Request  $request
125
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
126
     * @return \Illuminate\Http\Response
127
     */
128 2
    public function update(Request $request, Blog $blog)
129
    {
130 2
        request()->validate([
131 2
            'category_id' => 'required',
132
            'layout' => 'required',
133
        ]);
134
135 1
        $this->saveOnDb($request, $blog);
136
137 1
        return redirect()->route('blogs.index')
138 1
                        ->with('success', __('messages.blog_updated_successfully'));
139
    }
140
141
    /**
142
     * Remove the specified resource from storage.
143
     *
144
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
145
     * @return \Illuminate\Http\Response
146
     */
147 1
    public function destroy(Blog $blog)
148
    {
149 1
        $blog->delete();
150
151 1
        return redirect()->route('blogs.index')
152 1
                        ->with('success', __('messages.blog_deleted_successfully'));
153
    }
154
155
    /***************************************************************************/
156
157
    /**
158
     * Save/Update the record on DB.
159
     *
160
     * @param  \Illuminate\Http\Request  $request
161
     * @return void
162
     */
163 2
    public function saveOnDb($request, $blog)
164
    {
165
        /* Blog layout */
166 2
        $blog->category_id = $request->get('category_id');
167 2
        $blog->layout = $request->get('layout');
168 2
        $blog->columns_number = $request->get('columns_number');
169 2
        $blog->columns_width = $request->get('columns_width');
170 2
        $blog->article_order = $request->get('article_order');
171
172 2
        $blog->items_per_page = $request->get('items_per_page');
173 2
        $blog->featured_articles = $request->get('featured_articles');
174 2
        $blog->show_category_title = ($request->show_category_title == 'on') ? 1 : 0;
175 2
        $blog->show_category_subtitle = ($request->show_category_subtitle == 'on') ? 1 : 0;
176 2
        $blog->show_category_description = ($request->show_category_description == 'on') ? 1 : 0;
177 2
        $blog->show_category_image = ($request->show_category_image == 'on') ? 1 : 0;
178
179 2
        $blog->show_post_title = ($request->show_post_title == 'on') ? 1 : 0;
180 2
        $blog->post_linked_titles = ($request->post_linked_titles == 'on') ? 1 : 0;
181 2
        $blog->show_post_intro_text = ($request->show_post_intro_text == 'on') ? 1 : 0;
182 2
        $blog->show_post_author = ($request->show_post_author == 'on') ? 1 : 0;
183 2
        $blog->link_post_author = ($request->link_post_author == 'on') ? 1 : 0;
184
185 2
        $blog->show_create_date = ($request->show_create_date == 'on') ? 1 : 0;
186 2
        $blog->show_modify_date = ($request->show_modify_date == 'on') ? 1 : 0;
187 2
        $blog->show_publish_date = ($request->show_publish_date == 'on') ? 1 : 0;
188 2
        $blog->show_read_more = ($request->show_read_more == 'on') ? 1 : 0;
189
190 2
        $blog->created_by = \Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
191
192 2
        $blog->save();
193 2
    }
194
}
195