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

ResponsiveQuoteController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
nc 1
nop 1
cc 1
crap 1
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\Facades\PhpResponsiveQuote;
9
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
10
11
class ResponsiveQuoteController
12
{
13
    /**
14
     * Display the specified resource.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @return \Illuminate\Http\Response
18
     */
19 2
    public function index(Request $request)
20
    {
21 2
        $searchKeywords = $request->input('keywords');
22
        //$searchCategory = $request->input('category_id');
23 2
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
24
25 2
        if ($searchKeywords) {
26
            $quotes = Quote::orderBy('author')
27
                                     ->where('author', 'like', '%'.$request->input('keywords').'%')
28
                                     ->paginate(20);
29
        } else {
30 2
            $quotes = Quote::orderBy('author')
31 2
                                     ->paginate(20);
32
        }
33
        
34 2
        return view('php-responsive-quote::quotes.index', compact('quotes'))
35 2
                             ->with('i', (request()->input('page', 1) - 1) * 20)
36 2
                             ->with('searchKeywords', $searchKeywords)
37 2
                             ->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 1
    public function create()
48
    {
49 1
        return view('php-responsive-quote::quotes.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 1
    public function store(Request $request)
61
    {
62 1
        $quote = new Quote();
63 1
        $quote->author = $request->get('author');
64 1
        $quote->text = $request->get('text');
0 ignored issues
show
Bug introduced by
The property text does not seem to exist on DavideCasiraghi\PhpRespo...andomQuote\Models\Quote. 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 1
        App::setLocale('en');
68
69 1
        $this->saveOnDb($request, $quote);
70
71 1
        return redirect()->route('php-responsive-quote.index')
72 1
                            ->with('success', 'Quote added succesfully');
73
    }
74
75
    /***************************************************************************/
76
77
    /**
78
     * Display the specified resource.
79
     *
80
     * @param  int $quoteId
81
     * @return \Illuminate\Http\Response
82
     */
83 1
    public function show($quoteId = null)
84
    {
85 1
        $quote = Quote::find($quoteId);
86
87 1
        return view('php-responsive-quote::quotes.show', compact('quote'));
88
    }
89
90
    /***************************************************************************/
91
92
    /**
93
     * Show the form for editing the specified resource.
94
     *
95
     * @param  int $quoteId
96
     * @return \Illuminate\Http\Response
97
     */
98 1
    public function edit($quoteId = null)
99
    {
100 1
        $quote = Quote::find($quoteId);
101
102 1
        return view('php-responsive-quote::quotes.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  $quoteId
112
     * @return \Illuminate\Http\Response
113
     */
114 1
    public function update(Request $request, $quoteId)
115
    {
116 1
        $quote = Quote::find($quoteId);
117
118
        // Set the default language to update the quote in English
119 1
        App::setLocale('en');
120
121 1
        $this->saveOnDb($request, $quote);
122
123 1
        return redirect()->route('php-responsive-quote.index')
124 1
                            ->with('success', 'Quote updated succesfully');
125
    }
126
127
    /***************************************************************************/
128
129
    /**
130
     * Remove the specified resource from storage.
131
     *
132
     * @param  int  $quoteId
133
     * @return \Illuminate\Http\Response
134
     */
135 1
    public function destroy($quoteId)
136
    {
137 1
        $quote = Quote::find($quoteId);
138 1
        $quote->delete();
139
140 1
        return redirect()->route('php-responsive-quote.index')
141 1
                            ->with('success', 'Quote deleted succesfully');
142
    }
143
144
    /***************************************************************************/
145
146
    /**
147
     * Save the record on DB.
148
     * @param  \Illuminate\Http\Request  $request
149
     * @param  \App\Quote  $quote
0 ignored issues
show
Bug introduced by
The type App\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 2
    public function saveOnDb($request, $quote)
153
    {
154 2
        $quote->translateOrNew('en')->text = $request->get('text');
155 2
        $quote->author = $request->get('author');
156 2
        $quote->save();
157 2
    }
158
159
    /***************************************************************************/
160
161
    /**
162
     * Display the specified resource.
163
     *
164
     * @return \Illuminate\Http\Response
165
     */
166 1
    public function showRandomQuote()
167
    {
168 1
        $quote = PhpResponsiveQuote::getRandomQuote();
0 ignored issues
show
Bug introduced by
The method getRandomQuote() does not exist on DavideCasiraghi\PhpRespo...ades\PhpResponsiveQuote. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        /** @scrutinizer ignore-call */ 
169
        $quote = PhpResponsiveQuote::getRandomQuote();
Loading history...
169
170
        // the view name is set in the - Service provider - boot - loadViewsFrom
171 1
        return view('php-responsive-quote::show-random-quote', [
172 1
            'quoteAuthor' => $quote['author'],
173 1
            'quoteText' => $quote['text'],
174
        ]);
175
    }
176
}
177