TestimonialTranslationController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
eloc 46
c 1
b 0
f 1
dl 0
loc 141
ccs 52
cts 52
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 12 1
A update() 0 20 2
A store() 0 16 2
A destroy() 0 7 1
A create() 0 8 1
A saveOnDb() 0 16 3
1
<?php
2
3
namespace DavideCasiraghi\LaravelTestimonials\Http\Controllers;
4
5
use DavideCasiraghi\LaravelTestimonials\Models\TestimonialTranslation;
6
use Illuminate\Http\Request;
7
use Validator;
8
9
class TestimonialTranslationController 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($testimonialId, $languageCode)
20
    {
21 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
22
23 1
        return view('laravel-testimonials::testimonialsTranslations.create')
24 1
                ->with('testimonialId', $testimonialId)
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($testimonialId, $languageCode)
39
    {
40 1
        $testimonialTranslation = TestimonialTranslation::where('testimonial_id', $testimonialId)
41 1
                        ->where('locale', $languageCode)
42 1
                        ->first();
43
44 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
45
46 1
        return view('laravel-testimonials::testimonialsTranslations.edit', compact('testimonialTranslation'))
47 1
                    ->with('testimonialId', $testimonialId)
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
            'name' => 'required',
65
        ]);
66 6
        if ($validator->fails()) {
67 1
            return back()->withErrors($validator)->withInput();
68
        }
69
70 5
        $testimonialTranslation = new TestimonialTranslation();
71
72 5
        $this->saveOnDb($request, $testimonialTranslation, 'save');
73
74 5
        return redirect()->route('testimonials.index')
75 5
                            ->with('success', 'Testimonial 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
            'name' => 'required',
92
        ]);
93 2
        if ($validator->fails()) {
94 1
            return back()->withErrors($validator)->withInput();
95
        }
96
97 1
        $testimonialTranslation = TestimonialTranslation::find($request->get('testimonial_translation_id'));
98
99
        //dd($testimonialTranslation);
100
        //$eventCategoryTranslation = EventCategoryTranslation::where('id', $request->get('event_category_translation_id'));
101
102
        //dd($testimonialTranslation);
103 1
        $this->saveOnDb($request, $testimonialTranslation, 'update');
104
105 1
        return redirect()->route('testimonials.index')
106 1
                            ->with('success', 'Testimonial translation added succesfully');
107
    }
108
109
    /***************************************************************************/
110
111
    /**
112
     * Save the record on DB.
113
     * @param  \Illuminate\Http\Request  $request
114
     * @param  \DavideCasiraghi\LaravelTestimonials\Models\TestimonialTranslation  $testimonialTranslation
115
     * @return void
116
     */
117 5
    public function saveOnDb($request, $testimonialTranslation, $saveOrUpdate)
118
    {
119
        //dd($testimonialTranslation);
120 5
        $testimonialTranslation->name = $request->get('name');
121 5
        $testimonialTranslation->body = $request->get('body');
122 5
        $testimonialTranslation->profession = $request->get('profession');
123
124 5
        switch ($saveOrUpdate) {
125 5
            case 'save':
126 5
                $testimonialTranslation->testimonial_id = $request->get('testimonial_id');
127 5
                $testimonialTranslation->locale = $request->get('language_code');
128 5
                $testimonialTranslation->save();
129 5
                break;
130 1
            case 'update':
131 1
                $testimonialTranslation->update();
132 1
                break;
133
        }
134 5
    }
135
136
    /***************************************************************************/
137
138
    /**
139
     * Remove the specified resource from storage.
140
     *
141
     * @param  int  $testimonialTranslationId
142
     */
143 1
    public function destroy($testimonialTranslationId)
144
    {
145 1
        $testimonialTranslation = TestimonialTranslation::find($testimonialTranslationId);
146 1
        $testimonialTranslation->delete();
147
148 1
        return redirect()->route('testimonials.index')
149 1
                            ->with('success', 'Testimonial translation deleted succesfully');
150
    }
151
}
152