ContactController::store()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 20
rs 9.8666
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\ContactMessage;
6
use Illuminate\Http\Request;
7
use App\Mail\ContactSubmitted;
8
use Illuminate\Support\Facades\Log;
9
use Illuminate\Support\Facades\Mail;
10
11
class ContactController extends Controller
12
{
13
    /**
14
     * Returns a view with the contact form
15
     *
16
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
17
     */
18
    public function show()
19
    {
20
        return view('contacts.create');
21
    }
22
23
    /**
24
     * Validates and submits a new contact form.
25
     * Sends an e-mail address if all went as expected.
26
     *
27
     * @param Request $request
28
     * @return \Illuminate\Http\RedirectResponse
29
     */
30
    public function store(Request $request)
31
    {
32
        $data = $request->validate([
33
            'name' => 'required|string|max:60',
34
            'email' => 'required|email',
35
            'message' => 'required|string|max:2000',
36
        ]);
37
38
        $name = $request->get('name');
39
        $contactMessage = ContactMessage::create($data);
40
        $recipientAddress = config('mail.to')['address'];
41
42
        if ($recipientAddress && (config('mail.status') || app()->environment(['production']))) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

42
        if ($recipientAddress && (config('mail.status') || app()->/** @scrutinizer ignore-call */ environment(['production']))) {
Loading history...
43
            // Send e-mail to admin alerting about new contact
44
            Mail::to($recipientAddress)->send(new ContactSubmitted($contactMessage));
45
            Log::info('New e-mail sent.');
46
        }
47
48
        return redirect()->route('home')
49
            ->with('success', "<p>Obrigado, $name!</p><p>A tua mensagem foi enviada com sucesso! Responderemos
50
                assim que possível.</p>");
51
    }
52
}
53