Completed
Push — master ( 70951f...1e2ebe )
by Davide
03:01
created

getSelectedLocaleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
8
9
class JumbotronImageTranslationController
10
{
11
    /***************************************************************************/
12
13
    /**
14
     * Show the form for creating a new resource.
15
     * @param int $quoteId
16
     * @param string $languageCode
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function create($quoteId, $languageCode)
20
    {
21
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
22
23
        return view('laravel-jumbotron-images::jumbotronImagesTranslations.create')
24
                ->with('quoteId', $quoteId)
25
                ->with('languageCode', $languageCode)
26
                ->with('selectedLocaleName', $selectedLocaleName);
27
    }
28
29
    /***************************************************************************/
30
31
    /**
32
     * Show the form for editing the specified resource.
33
     *
34
     * @param int $quoteId
35
     * @param string $languageCode
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function edit($quoteId, $languageCode)
39
    {
40
        $quoteTranslation = QuoteTranslation::where('quote_id', $quoteId)
0 ignored issues
show
Bug introduced by
The type DavideCasiraghi\LaravelJ...ollers\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...
41
                        ->where('locale', $languageCode)
42
                        ->first();
43
44
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
45
46
        return view('laravel-jumbotron-images::jumbotronImagesTranslations.edit', compact('quoteTranslation'))
47
                    ->with('quoteId', $quoteId)
48
                    ->with('languageCode', $languageCode)
49
                    ->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
    public function store(Request $request)
61
    {
62
63
        // Validate form datas
64
        $validator = Validator::make($request->all(), [
65
                'text' => 'required',
66
            ]);
67
        if ($validator->fails()) {
68
            return back()->withErrors($validator)->withInput();
69
        }
70
71
        $quoteTranslation = new QuoteTranslation();
72
73
        $this->saveOnDb($request, $quoteTranslation);
74
75
        return redirect()->route('jumbotron-images.index')
76
                            ->with('success', 'Quote translation added succesfully');
77
    }
78
79
    /***************************************************************************/
80
81
    /**
82
     * Update the specified resource in storage.
83
     *
84
     * @param  \Illuminate\Http\Request  $request
85
     * @param  int  $quoteTranslationId
86
     * @return \Illuminate\Http\Response
87
     */
88
    public function update(Request $request, $quoteTranslationId)
89
    {
90
        request()->validate([
91
            'text' => 'required',
92
        ]);
93
94
        $quoteTranslation = QuoteTranslation::find($quoteTranslationId);
95
96
        $this->saveOnDb($request, $quoteTranslation);
97
98
        return redirect()->route('jumbotron-images.index')
99
                            ->with('success', 'Quote translation added succesfully');
100
    }
101
102
    /***************************************************************************/
103
104
    /**
105
     * Save the record on DB.
106
     * @param  \Illuminate\Http\Request  $request
107
     * @param  \DavideCasiraghi\PhpResponsiveRandomQuote\Models\QuoteTranslation  $quoteTranslation
0 ignored issues
show
Bug introduced by
The type DavideCasiraghi\PhpRespo...Models\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...
108
     * @return void
109
     */
110
    public function saveOnDb($request, $quoteTranslation)
111
    {
112
        $quoteTranslation->quote_id = $request->get('quote_id');
113
        $quoteTranslation->locale = $request->get('language_code');
114
115
        $quoteTranslation->text = $request->get('text');
116
        $quoteTranslation->save();
117
    }
118
119
    /***************************************************************************/
120
121
    /**
122
     * Get the language name from language code.
123
     *
124
     * @param  string $languageCode
125
     * @return string
126
     */
127
    public function getSelectedLocaleName($languageCode)
128
    {
129
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
130
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
131
132
        return $ret;
133
    }
134
135
    /***************************************************************************/
136
137
    /**
138
     * Remove the specified resource from storage.
139
     *
140
     * @param  int  $quoteTranslationId
141
     */
142
    public function destroy($quoteTranslationId)
143
    {
144
        $quoteTranslation = QuoteTranslation::find($quoteTranslationId);
145
        $quoteTranslation->delete();
146
147
        return redirect()->route('jumbotron-images.index')
148
                            ->with('success', 'Quote translation deleted succesfully');
149
    }
150
}
151