CategoryTranslationController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 149
ccs 48
cts 48
cp 1
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 17 1
A destroy() 0 7 1
A store() 0 22 2
A edit() 0 12 1
A __construct() 0 3 1
A create() 0 8 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\CategoryTranslation;
9
10
class CategoryTranslationController 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
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    /*public function index()
27
    {
28
        //
29
    }*/
30
31
    /***************************************************************************/
32
33
    /**
34
     * Show the form for creating a new resource.
35
     * @param int $categoryId
36
     * @param string $languageCode
37
     * @return \Illuminate\Http\Response
38
     */
39 1
    public function create($categoryId, $languageCode)
40
    {
41 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
42
43 1
        return view('laravel-smart-blog::categoryTranslations.create')
44 1
                ->with('categoryId', $categoryId)
45 1
                ->with('languageCode', $languageCode)
46 1
                ->with('selectedLocaleName', $selectedLocaleName);
47
    }
48
49
    // **********************************************************************
50
51
    /**
52
     * Show the form for editing the specified resource.
53
     *
54
     * @param int $categoryId
55
     * @param string $languageCode
56
     * @return \Illuminate\Http\Response
57
     */
58 1
    public function edit($categoryId, $languageCode)
59
    {
60 1
        $categoryTranslation = CategoryTranslation::where('category_id', $categoryId)
61 1
                        ->where('locale', $languageCode)
62 1
                        ->first();
63
64 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
65
66 1
        return view('laravel-smart-blog::categoryTranslations.edit', compact('categoryTranslation'))
67 1
                    ->with('categoryId', $categoryId)
68 1
                    ->with('languageCode', $languageCode)
69 1
                    ->with('selectedLocaleName', $selectedLocaleName);
70
    }
71
72
    /***************************************************************************/
73
74
    /**
75
     * Store a newly created resource in storage.
76
     *
77
     * @param  \Illuminate\Http\Request  $request
78
     * @return \Illuminate\Http\Response
79
     */
80 6
    public function store(Request $request)
81
    {
82
        // Validate form datas
83 6
        $validator = Validator::make($request->all(), [
84 6
                'name' => 'required',
85
            ]);
86 6
        if ($validator->fails()) {
87 1
            return back()->withErrors($validator)->withInput();
88
        }
89
90 5
        $categoryTranslation = new CategoryTranslation();
91 5
        $categoryTranslation->category_id = $request->get('category_id');
92 5
        $categoryTranslation->locale = $request->get('language_code');
93
94 5
        $categoryTranslation->name = $request->get('name');
95 5
        $categoryTranslation->description = $request->get('description');
96 5
        $categoryTranslation->slug = Str::slug($categoryTranslation->name, '-');
97
98 5
        $categoryTranslation->save();
99
100 5
        return redirect()->route('categories.index')
101 5
                        ->with('success', __('laravel-smart-blog::messages.category_translation_added_successfully'));
102
    }
103
104
    /***************************************************************************/
105
106
    /**
107
     * Display the specified resource.
108
     *
109
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\CategoryTranslation  $categoryTranslation
110
     * @return \Illuminate\Http\Response
111
     */
112
    /*public function show(CategoryTranslation $categoryTranslation)
113
    {
114
        //
115
    }*/
116
117
    /***************************************************************************/
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param  \Illuminate\Http\Request  $request
123
     * @return \Illuminate\Http\Response
124
     */
125 2
    public function update(Request $request)
126
    {
127 2
        request()->validate([
128 2
            'name' => 'required',
129
        ]);
130
131 1
        $categoryTranslation = CategoryTranslation::where('id', $request->get('category_translation_id'));
132
133 1
        $category_t = [];
134 1
        $category_t['name'] = $request->get('name');
135 1
        $category_t['description'] = $request->get('description');
136 1
        $category_t['slug'] = Str::slug($request->get('name'), '-');
137
138 1
        $categoryTranslation->update($category_t);
139
140 1
        return redirect()->route('categories.index')
141 1
                        ->with('success', __('laravel-smart-blog::messages.category_translation_updated_successfully'));
142
    }
143
144
    /***************************************************************************/
145
146
    /**
147
     * Remove the specified resource from storage.
148
     *
149
     * @param int $categoryTranslationId
150
     * @return \Illuminate\Http\Response
151
     */
152 1
    public function destroy($categoryTranslationId)
153
    {
154 1
        $categoryTranslation = CategoryTranslation::find($categoryTranslationId);
155 1
        $categoryTranslation->delete();
156
157 1
        return redirect()->route('categories.index')
158 1
                        ->with('success', __('laravel-smart-blog::messages.category_translation_deleted_successfully'));
159
    }
160
}
161