Completed
Push — master ( d82dbc...4763a5 )
by Davide
06:58
created

JumbotronImagesTranslationController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 140
c 0
b 0
f 0
rs 10
wmc 8

7 Methods

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