PostTranslationController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 50
c 2
b 0
f 2
dl 0
loc 135
ccs 54
cts 54
cp 1
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 7 1
A edit() 0 12 1
A __construct() 0 3 1
A create() 0 8 1
A store() 0 29 2
A update() 0 21 1
1
<?php
2
3
namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use DavideCasiraghi\LaravelSmartBlog\Models\PostTranslation;
9
10
class PostTranslationController extends Controller
11
{
12
    /***************************************************************************/
13
    /* Restrict the access to this resource just to logged in users */
14 7
    public function __construct()
15
    {
16 7
        $this->middleware('admin');
17 7
    }
18
19
    /***************************************************************************/
20
21
    /**
22
     * Show the form for creating a new resource.
23
     * @param int $postId
24
     * @param string $languageCode
25
     * @return \Illuminate\Http\Response
26
     */
27 1
    public function create($postId, $languageCode)
28
    {
29 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
30
31 1
        return view('laravel-smart-blog::postTranslations.create')
32 1
                ->with('postId', $postId)
33 1
                ->with('languageCode', $languageCode)
34 1
                ->with('selectedLocaleName', $selectedLocaleName);
35
    }
36
37
    /***************************************************************************/
38
39
    /**
40
     * Show the form for editing the specified resource.
41
     *
42
     * @param int $postId
43
     * @param string $languageCode
44
     * @return \Illuminate\Http\Response
45
     */
46 1
    public function edit($postId, $languageCode)
47
    {
48 1
        $postTranslation = PostTranslation::where('post_id', $postId)
49 1
                        ->where('locale', $languageCode)
50 1
                        ->first();
51
52 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
53
54 1
        return view('laravel-smart-blog::postTranslations.edit', compact('postTranslation'))
55 1
                    ->with('postId', $postId)
56 1
                    ->with('languageCode', $languageCode)
57 1
                    ->with('selectedLocaleName', $selectedLocaleName);
58
    }
59
60
    /***************************************************************************/
61
62
    /**
63
     * Store a newly created resource in storage.
64
     *
65
     * @param  \Illuminate\Http\Request  $request
66
     * @return \Illuminate\Http\Response
67
     */
68 6
    public function store(Request $request)
69
    {
70
71
        // Validate form datas
72 6
        $validator = Validator::make($request->all(), [
73 6
                'title' => 'required',
74
                'body' => 'required',
75
            ]);
76 6
        if ($validator->fails()) {
77 1
            return back()->withErrors($validator)->withInput();
78
        }
79
80 5
        $postTranslation = new PostTranslation();
81
82 5
        $postTranslation->post_id = $request->get('post_id');
83 5
        $postTranslation->locale = $request->get('language_code');
84
85 5
        $postTranslation->title = $request->get('title');
86 5
        $postTranslation->body = clean($request->get('body'));
87 5
        $postTranslation->intro_text = clean($request->get('intro_text'));
88 5
        $postTranslation->slug = Str::slug($postTranslation->title, '-');
89
90 5
        $postTranslation->before_content = $request->get('before_content');
91 5
        $postTranslation->after_content = $request->get('after_content');
92
93 5
        $postTranslation->save();
94
95 5
        return redirect()->route('posts.index')
96 5
                        ->with('success', __('laravel-smart-blog::messages.article_translation_added_successfully'));
97
    }
98
99
    /***************************************************************************/
100
101
    /**
102
     * Update the specified resource in storage.
103
     *
104
     * @param  \Illuminate\Http\Request  $request
105
     * @return \Illuminate\Http\Response
106
     */
107 2
    public function update(Request $request)
108
    {
109 2
        request()->validate([
110 2
            'title' => 'required',
111
            'body' => 'required',
112
        ]);
113
114 1
        $postTranslation = PostTranslation::where('id', $request->get('post_translation_id'));
115
116 1
        $pt = [];
117 1
        $pt['title'] = $request->get('title');
118 1
        $pt['body'] = clean($request->get('body'));
119 1
        $pt['intro_text'] = clean($request->get('intro_text'));
120 1
        $pt['slug'] = Str::slug($request->get('title'), '-');
121 1
        $pt['before_content'] = $request->get('before_content');
122 1
        $pt['after_content'] = $request->get('after_content');
123
124 1
        $postTranslation->update($pt);
125
126 1
        return redirect()->route('posts.index')
127 1
                        ->with('success', __('laravel-smart-blog::messages.article_translation_updated_successfully'));
128
    }
129
130
    /***************************************************************************/
131
132
    /**
133
     * Remove the specified resource from storage.
134
     *
135
     * @param  int $postTranslationId
136
     * @return \Illuminate\Http\Response
137
     */
138 1
    public function destroy($postTranslationId)
139
    {
140 1
        $postTranslation = PostTranslation::find($postTranslationId);
141 1
        $postTranslation->delete();
142
143 1
        return redirect()->route('posts.index')
144 1
                        ->with('success', __('laravel-smart-blog::messages.post_translation_deleted_successfully'));
145
    }
146
}
147