Zrobiłem to co opisałem w temacie. Kod działa. Ale chcę napisać czysty kod zgodny z SOLID taki aby nie można było się do niczego przyczepić i nie jestem pewny czy mi wyszło dlatego proszę o ocenę
Kontroler
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Interfaces\NotificationValidator;
use App\Interfaces\Notifier;
use App\Notifications\SendEmail;
use App\Mail\SimpleEmail;
class SendMessageFromUserController extends Controller
{
public function sendMessageFromUser(Request $request)
{
$data = $request->validate([
'email' => 'max:30',
'subject' => 'max:30',
'message' => 'required|min:3|max:1000'
]);
$notification = new SendEmail(new SimpleEmail($data['message'], $data['subject'], $data['email']),
config('mail.management_email'));
return $notification->notify();
}
}
Klasa budująca maila
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SimpleEmail extends Mailable
{
use Queueable, SerializesModels;
private $sender;
private $msg;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($msg, $subject = "Brak tematu",$sender = "Brak adresu")
{
$this->subject = empty($subject) ? "Brak tematu" : $subject;
$this->sender = empty($sender) ? "Brak adresu" : $sender;
$this->msg = $msg;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from($this->sender)
->view('mails.simple_email')
->subject($this->subject)
->with(['messageContent' => $this->msg]);
}
}
Klasa wysyłająca maila
<?php
namespace App\Notifications;
use App\Interfaces\Notifier;
use App\Mail\MessageFromUser;
use Mail;
use Illuminate\Mail\Mailable;
class SendEmail implements Notifier
{
private $mail;
private $receipient;
public function __construct(Mailable $mail,$receipient)
{
$this->mail = $mail;
$this->receipient = $receipient;
}
public function notify()
{
Mail::to($this->receipient)->send($this->mail);
return Mail::failures() ? back()->withErrors(['Nieoczekiwany błąd podczas wysyłania wiadomości']) : back()->with('success','Pomyślnie wysłano wiadomość');
}
}
Czy nie wieje tutaj amatorszczyzną?