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

showRandomJumbotronImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 0
dl 0
loc 8
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\App;
7
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage;
8
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
9
use DavideCasiraghi\LaravelJumbotronImages\Facades\LaravelJumbotronImagesFacade;
10
11
class JumbotronImagesController
12
{
13
    /**
14
     * Display the specified resource.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function index(Request $request)
20
    {
21
        $searchKeywords = $request->input('keywords');
22
        //$searchCategory = $request->input('category_id');
23
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
24
25
        if ($searchKeywords) {
26
            $jumbotronImages = JumbotronImage::orderBy('author')
27
                                     ->where('author', 'like', '%'.$request->input('keywords').'%')
28
                                     ->paginate(20);
29
        } else {
30
            $jumbotronImages = JumbotronImage::orderBy('author')
31
                                     ->paginate(20);
32
        }
33
34
        return view('laravel-jumbotron-images::jumbotronImages.index', compact('jumbotronImages'))
35
                             ->with('i', (request()->input('page', 1) - 1) * 20)
36
                             ->with('searchKeywords', $searchKeywords)
37
                             ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
38
    }
39
40
    /***************************************************************************/
41
42
    /**
43
     * Show the form for creating a new resource.
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function create()
48
    {
49
        return view('laravel-jumbotron-images::jumbotronImages.create');
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
        $jumbotronImage = new JumbotronImage();
63
        $jumbotronImage->author = $request->get('author');
0 ignored issues
show
Bug introduced by
The property author does not seem to exist on DavideCasiraghi\LaravelJ...s\Models\JumbotronImage. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
64
        $jumbotronImage->text = $request->get('text');
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on DavideCasiraghi\LaravelJ...s\Models\JumbotronImage. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
65
66
        // Set the default language to edit the quote in English
67
        App::setLocale('en');
68
69
        $this->saveOnDb($request, $jumbotronImage);
70
71
        return redirect()->route('jumbotron-images.index')
72
                            ->with('success', 'Quote added succesfully');
73
    }
74
75
    /***************************************************************************/
76
77
    /**
78
     * Display the specified resource.
79
     *
80
     * @param  int $jumbotronImageId
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function show($jumbotronImageId = null)
84
    {
85
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
0 ignored issues
show
Unused Code introduced by
The assignment to $jumbotronImage is dead and can be removed.
Loading history...
86
87
        return view('laravel-jumbotron-images::jumbotronImages.show', compact('quote'));
88
    }
89
90
    /***************************************************************************/
91
92
    /**
93
     * Show the form for editing the specified resource.
94
     *
95
     * @param  int $jumbotronImageId
96
     * @return \Illuminate\Http\Response
97
     */
98
    public function edit($jumbotronImageId = null)
99
    {
100
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
0 ignored issues
show
Unused Code introduced by
The assignment to $jumbotronImage is dead and can be removed.
Loading history...
101
102
        return view('laravel-jumbotron-images::jumbotronImages.edit', compact('quote'));
103
    }
104
105
    /***************************************************************************/
106
107
    /**
108
     * Update the specified resource in storage.
109
     *
110
     * @param  \Illuminate\Http\Request  $request
111
     * @param  int  $jumbotronImageId
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function update(Request $request, $jumbotronImageId)
115
    {
116
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
117
118
        // Set the default language to update the quote in English
119
        App::setLocale('en');
120
121
        $this->saveOnDb($request, $jumbotronImage);
122
123
        return redirect()->route('jumbotron-images.index')
124
                            ->with('success', 'Quote updated succesfully');
125
    }
126
127
    /***************************************************************************/
128
129
    /**
130
     * Remove the specified resource from storage.
131
     *
132
     * @param  int  $jumbotronImageId
133
     * @return \Illuminate\Http\Response
134
     */
135
    public function destroy($jumbotronImageId)
136
    {
137
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
138
        $jumbotronImage->delete();
139
140
        return redirect()->route('jumbotron-images.index')
141
                            ->with('success', 'Quote deleted succesfully');
142
    }
143
144
    /***************************************************************************/
145
146
    /**
147
     * Save the record on DB.
148
     * @param  \Illuminate\Http\Request  $request
149
     * @param  \DavideCasiraghi\PhpResponsiveRandomQuote\Models\Quote  $jumbotronImage
0 ignored issues
show
Bug introduced by
The type DavideCasiraghi\PhpRespo...andomQuote\Models\Quote 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...
150
     * @return void
151
     */
152
    public function saveOnDb($request, $jumbotronImage)
153
    {
154
        $jumbotronImage->translateOrNew('en')->text = $request->get('text');
155
        $jumbotronImage->author = $request->get('author');
156
        $jumbotronImage->save();
157
    }
158
159
    /***************************************************************************/
160
161
    /**
162
     * Display the specified resource.
163
     *
164
     * @return \Illuminate\Http\Response
165
     */
166
    public function showRandomJumbotronImage()
167
    {
168
        $jumbotronImage = PhpResponsiveJumbotronImage::getRandomJumbotronImage();
0 ignored issues
show
Bug introduced by
The type DavideCasiraghi\LaravelJ...esponsiveJumbotronImage 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...
169
170
        // the view name is set in the - Service provider - boot - loadViewsFrom
171
        return view('php-responsive-JumbotronImage::show-random-quote', [
172
            'quoteAuthor' => $jumbotronImage['author'],
173
            'quoteText' => $jumbotronImage['text'],
174
        ]);
175
    }
176
}
177