Completed
Push — master ( 5fdac1...20b91e )
by Davide
03:59
created

ResponsiveQuoteTranslationController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
nc 1
nop 2
cc 1
crap 2
1
<?php
2
3
namespace DavideCasiraghi\PhpResponsiveRandomQuote\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\App;
7
use DavideCasiraghi\PhpResponsiveRandomQuote\Models\Quote;
8
use DavideCasiraghi\PhpResponsiveRandomQuote\Models\QuoteTranslation;
9
use DavideCasiraghi\PhpResponsiveRandomQuote\Facades\PhpResponsiveQuote;
10
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
11
use Validator;
12
13
class ResponsiveQuoteTranslationController
14
{
15
    /***************************************************************************/
16
17
    /**
18
     * Show the form for creating a new resource.
19
     * @param int $quoteId
20
     * @param string $languageCode
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function create($quoteId, $languageCode)
24
    {
25
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
26
27
        return view('php-responsive-quote::quoteTranslations.create')
28
                ->with('quoteId', $quoteId)
29
                ->with('languageCode', $languageCode)
30
                ->with('selectedLocaleName', $selectedLocaleName);
31
    }
32
33
    /***************************************************************************/
34
    
35
    /**
36
     * Show the form for editing the specified resource.
37
     *
38
     * @param int $quoteId
39
     * @param string $languageCode
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function edit($quoteId, $languageCode)
43
    {
44
        $quoteTranslation = QuoteTranslation::where('quote_id', $quoteId)
45
                        ->where('locale', $languageCode)
46
                        ->first();
47
48
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
49
50
        return view('php-responsive-quote::quoteTranslations.edit', compact('quoteTranslation'))
51
                    ->with('quoteId', $quoteId)
52
                    ->with('languageCode', $languageCode)
53
                    ->with('selectedLocaleName', $selectedLocaleName);
54
    }
55
    
56
    /***************************************************************************/
57
58
    /**
59
     * Store a newly created resource in storage.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     * @return \Illuminate\Http\Response
63
     */
64
    public function store(Request $request)
65
    {
66
67
        // Validate form datas
68
        $validator = Validator::make($request->all(), [
69
                'text' => 'required',
70
            ]);
71
        if ($validator->fails()) {
72
            return back()->withErrors($validator)->withInput();
73
        }
74
75
        $quoteTranslation = new QuoteTranslation();
76
77
        $this->saveOnDb($request, $quoteTranslation);
78
79
        return redirect()->route('php-responsive-quote.index')
80
                            ->with('success', 'Quote translation added succesfully');
81
    }
82
    /***************************************************************************/
83
84
    /**
85
     * Update the specified resource in storage.
86
     *
87
     * @param  \Illuminate\Http\Request  $request
88
     * @param  int  $quoteTranslationId
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function update(Request $request, $quoteTranslationId)
92
    {
93
        request()->validate([
94
            'text' => 'required',
95
        ]);
96
        
97
        $quoteTranslation = QuoteTranslation::find($quoteTranslationId);
98
        
99
        $this->saveOnDb($request, $quoteTranslation);
100
101
        return redirect()->route('php-responsive-quote.index')
102
                            ->with('success', 'Quote translation added succesfully');
103
    }
104
        
105
    /***************************************************************************/
106
107
    /**
108
     * Save the record on DB.
109
     * @param  \Illuminate\Http\Request  $request
110
     * @param  \App\QuoteTranslation  $quoteTranslation
0 ignored issues
show
Bug introduced by
The type App\QuoteTranslation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
     * @return void
112
     */
113
    public function saveOnDb($request, $quoteTranslation)
114
    {
115
        $quoteTranslation->quote_id = $request->get('quote_id');
116
        $quoteTranslation->locale = $request->get('language_code');
117
            
118
        $quoteTranslation->text = $request->get('text');
119
        $quoteTranslation->save();
120
    }
121
122
    /***************************************************************************/
123
124
    /**
125
     * Get the language name from language code.
126
     *
127
     * @param  string $languageCode
128
     * @return string
129
     */
130
    public function getSelectedLocaleName($languageCode)
131
    {
132
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
133
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
134
135
        return $ret;
136
    }
137
138
    /***************************************************************************/
139
140
    /**
141
     * Remove the specified resource from storage.
142
     *
143
     * @param  int  $quoteTranslationId
144
     */
145
    public function destroy($quoteTranslationId)
146
    {
147
        $quoteTranslation = QuoteTranslation::find($quoteTranslationId);
148
        $quoteTranslation->delete();
149
150
        return redirect()->route('php-responsive-quote.index')
151
                            ->with('success', 'Quote translation deleted succesfully');
152
    }
153
    
154
}
155