TestimonialGroupTranslationController::store()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 10
nc 2
nop 1
cc 2
crap 2
1
<?php
2
3
namespace DavideCasiraghi\LaravelTestimonials\Http\Controllers;
4
5
use DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroupTranslation;
6
use Illuminate\Http\Request;
7
use Validator;
8
9
class TestimonialGroupTranslationController extends Controller
10
{
11
    /***************************************************************************/
12
13
    /**
14
     * Show the form for creating a new resource.
15
     * @param int $testimonialId
16
     * @param string $languageCode
17
     * @return \Illuminate\Http\Response
18
     */
19 1
    public function create($testimonialGroupId, $languageCode)
20
    {
21 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
22
23 1
        return view('laravel-testimonials::testimonialGroupTranslations.create')
24 1
                ->with('testimonialGroupId', $testimonialGroupId)
25 1
                ->with('languageCode', $languageCode)
26 1
                ->with('selectedLocaleName', $selectedLocaleName);
27
    }
28
29
    /***************************************************************************/
30
31
    /**
32
     * Show the form for editing the specified resource.
33
     *
34
     * @param int $testimonialTranslationId
35
     * @param string $languageCode
36
     * @return \Illuminate\Http\Response
37
     */
38 1
    public function edit($testimonialGroupId, $languageCode)
39
    {
40 1
        $testimonialGroupTranslation = TestimonialGroupTranslation::where('testimonial_group_id', $testimonialGroupId)
41 1
                        ->where('locale', $languageCode)
42 1
                        ->first();
43
44 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
45
46 1
        return view('laravel-testimonials::testimonialGroupTranslations.edit', compact('testimonialGroupTranslation'))
47 1
                    ->with('testimonialGroupId', $testimonialGroupId)
48 1
                    ->with('languageCode', $languageCode)
49 1
                    ->with('selectedLocaleName', $selectedLocaleName);
50
    }
51
52
    /***************************************************************************/
53
54
    /**
55
     * Store a newly created resource in storage.
56
     *
57
     * @param  \Illuminate\Http\Request  $request
58
     * @return \Illuminate\Http\Response
59
     */
60 6
    public function store(Request $request)
61
    {
62
        // Validate form datas
63 6
        $validator = Validator::make($request->all(), [
64 6
            'title' => 'required',
65
        ]);
66 6
        if ($validator->fails()) {
67 1
            return back()->withErrors($validator)->withInput();
68
        }
69
70 5
        $testimonialGroupTranslation = new TestimonialGroupTranslation();
71
72 5
        $this->saveOnDb($request, $testimonialGroupTranslation, 'save');
73
74 5
        return redirect()->route('testimonialGroups.index')
75 5
                            ->with('success', 'Testimonial group translation added succesfully');
76
    }
77
78
    /***************************************************************************/
79
80
    /**
81
     * Update the specified resource in storage.
82
     *
83
     * @param  \Illuminate\Http\Request  $request
84
     * @param  int  $testimonialTranslationId
85
     * @return \Illuminate\Http\Response
86
     */
87 2
    public function update(Request $request)
88
    {
89
        // Validate form datas
90 2
        $validator = Validator::make($request->all(), [
91 2
            'title' => 'required',
92
        ]);
93 2
        if ($validator->fails()) {
94 1
            return back()->withErrors($validator)->withInput();
95
        }
96
97 1
        $testimonialGroupTranslation = TestimonialGroupTranslation::find($request->get('testimonial_group_translation_id'));
98
99
        //dd($testimonialGroupTranslation);
100 1
        $this->saveOnDb($request, $testimonialGroupTranslation, 'update');
101
102 1
        return redirect()->route('testimonialGroups.index')
103 1
                            ->with('success', 'Testimonial group translation added succesfully');
104
    }
105
106
    /***************************************************************************/
107
108
    /**
109
     * Save the record on DB.
110
     * @param  \Illuminate\Http\Request  $request
111
     * @param  \DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroupTranslation  $testimonialGroupTranslation
112
     * @return void
113
     */
114 5
    public function saveOnDb($request, $testimonialGroupTranslation, $saveOrUpdate)
115
    {
116
        //dd($testimonialTranslation);
117 5
        $testimonialGroupTranslation->title = $request->get('title');
118
119 5
        switch ($saveOrUpdate) {
120 5
            case 'save':
121 5
                $testimonialGroupTranslation->testimonial_group_id = $request->get('testimonial_group_id');
122 5
                $testimonialGroupTranslation->locale = $request->get('language_code');
123 5
                $testimonialGroupTranslation->save();
124 5
                break;
125 1
            case 'update':
126 1
                $testimonialGroupTranslation->update();
127 1
                break;
128
        }
129 5
    }
130
131
    /***************************************************************************/
132
133
    /**
134
     * Remove the specified resource from storage.
135
     *
136
     * @param  int  $testimonialGroupTranslationId
137
     */
138 1
    public function destroy($testimonialGroupTranslationId)
139
    {
140 1
        $testimonialGroupTranslation = TestimonialGroupTranslation::find($testimonialGroupTranslationId);
141 1
        $testimonialGroupTranslation->delete();
142
143 1
        return redirect()->route('testimonialGroups.index')
144 1
                            ->with('success', 'Testimonial group translation deleted succesfully');
145
    }
146
}
147