TestimonialController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 138
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 15 1
A destroy() 0 8 1
A update() 0 8 1
A edit() 0 12 1
A create() 0 8 1
A __construct() 0 8 1
A store() 0 14 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Helpers\Helper;
6
use App\Http\Requests\TestimonialSearchRequest;
7
use App\Http\Requests\TestimonialStoreRequest;
8
use App\Models\Testimonial;
9
use App\Models\User;
10
use App\Notifications\NewTestimonialMailNotification;
11
use App\Services\CountryService;
12
use App\Services\NotificationService;
13
use App\Services\TestimonialService;
14
use App\Traits\CheckPermission;
15
use Illuminate\Http\RedirectResponse;
16
use Illuminate\Support\Facades\Auth;
17
use Illuminate\View\View;
18
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
19
20
class TestimonialController extends Controller
21
{
22
    use CheckPermission;
0 ignored issues
show
Bug introduced by
The trait App\Traits\CheckPermission requires the property $user_id which is not provided by App\Http\Controllers\TestimonialController.
Loading history...
23
24
    private TestimonialService $testimonialService;
25
    private CountryService $countryService;
26
    private NotificationService $notificationService;
27
28
    public function __construct(
29
        TestimonialService $testimonialService,
30
        CountryService $countryService,
31
        NotificationService $notificationService
32
    ) {
33
        $this->testimonialService = $testimonialService;
34
        $this->countryService = $countryService;
35
        $this->notificationService = $notificationService;
36
    }
37
38
    /**
39
     * Display a listing of the resource.
40
     *
41
     * @param \App\Http\Requests\TestimonialSearchRequest $request
42
     *
43
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
44
     */
45
    public function index(TestimonialSearchRequest $request): View
46
    {
47
        $this->checkPermission('testimonials.view');
48
49
        $searchParameters = Helper::getSearchParameters($request, Testimonial::SEARCH_PARAMETERS);
0 ignored issues
show
Bug introduced by
The constant App\Models\Testimonial::SEARCH_PARAMETERS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
50
51
        $testimonials = $this->testimonialService->getTestimonials(20, $searchParameters);
52
        $statuses = Testimonial::PUBLISHING_STATUS;
0 ignored issues
show
Bug introduced by
The constant App\Models\Testimonial::PUBLISHING_STATUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
        $countries = $this->countryService->getCountries();
54
55
        return view('testimonials.index', [
56
            'testimonials' => $testimonials,
57
            'searchParameters' => $searchParameters,
58
            'statuses' => $statuses,
59
            'countries' => $countries,
60
        ]);
61
    }
62
63
    /**
64
     * Show the form for creating a new resource.
65
     *
66
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
67
     */
68
    public function create(): View
69
    {
70
        // No permission since has to be possible for a guest use to create a testimonial
71
72
        $countries = $this->countryService->getCountries();
73
74
        return view('testimonials.create', [
75
            'countries' => $countries,
76
        ]);
77
    }
78
79
    /**
80
     * Store a newly created resource in storage.
81
     *
82
     * @param \App\Http\Requests\TestimonialStoreRequest $request
83
     *
84
     * @return \Illuminate\Http\RedirectResponse
85
     */
86
    public function store(TestimonialStoreRequest $request): RedirectResponse
87
    {
88
        $this->checkPermission('testimonials.create');
89
90
        $testimonial = $this->testimonialService->createTestimonial($request);
0 ignored issues
show
Unused Code introduced by
The assignment to $testimonial is dead and can be removed.
Loading history...
91
92
        $this->notificationService->sendEmailNewTestimonial($request->all(), 1);
93
94
        $message = Auth::guest() ? 'Thanks for your testimony!' : 'Testimonial created successfully';
95
96
        if (Auth::guest()) {
97
            return redirect()->route('testimonials.create')->with('success', $message);
98
        } else {
99
            return redirect()->route('testimonials.index')->with('success', $message);
100
        }
101
    }
102
103
    /**
104
     * Show the form for editing the specified resource.
105
     *
106
     * @param int $testimonialId
107
     *
108
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
109
     */
110
    public function edit(int $testimonialId)
111
    {
112
        $this->checkPermission('testimonials.edit');
113
114
        $testimonial = $this->testimonialService->getById($testimonialId);
115
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
116
        $countries = $this->countryService->getCountries();
117
118
        return view('testimonials.edit', [
119
            'testimonial' => $testimonial,
120
            'countriesAvailableForTranslations' => $countriesAvailableForTranslations,
121
            'countries' => $countries,
122
        ]);
123
    }
124
125
    /**
126
     * Update the specified resource in storage.
127
     *
128
     * @param \App\Http\Requests\TestimonialStoreRequest $request
129
     * @param int $testimonialId
130
     *
131
     * @return \Illuminate\Http\RedirectResponse
132
     */
133
    public function update(TestimonialStoreRequest $request, int $testimonialId): RedirectResponse
134
    {
135
        $this->checkPermission('testimonials.edit');
136
137
        $testimonial = $this->testimonialService->updateTestimonial($request, $testimonialId);
0 ignored issues
show
Unused Code introduced by
The assignment to $testimonial is dead and can be removed.
Loading history...
138
139
        return redirect()->route('testimonials.index')
140
            ->with('success', 'Testimonial updated successfully');
141
    }
142
143
    /**
144
     * Remove the specified resource from storage.
145
     *
146
     * @param int $testimonialId
147
     *
148
     * @return \Illuminate\Http\RedirectResponse
149
     */
150
    public function destroy(int $testimonialId): RedirectResponse
151
    {
152
        $this->checkPermission('testimonials.delete');
153
154
        $this->testimonialService->deleteTestimonial($testimonialId);
155
156
        return redirect()->route('testimonials.index')
157
            ->with('success', 'Testimonial deleted successfully');
158
    }
159
}
160