File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Kms/Development/PreviewController.php
<?php
namespace App\Komma\Kms\Development;
use App\Komma\Base\Controller;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Shop\Invoicing\InvoiceService;
use App\Komma\Shop\Orders\Mail\OrderPayLaterCustomer;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedCustomer;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedStaff;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Users\Models\KmsUser;
use App\Komma\Users\Models\SiteUser;
use App\Notifications\CustomerRegistered;
use App\Notifications\CustomerRegisteredSetPassword;
use App\Notifications\KmsResetPasswordNotification;
use App\Notifications\SiteResetPasswordNotification;
use Illuminate\Mail\Markdown;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\View\View;
class PreviewController extends Controller
{
/** @var InvoiceService */
private $invoiceService;
public function __construct()
{
$this->invoiceService = app(InvoiceService::class);
}
/*
|--------------------------------------------------------------------------
| Mail previews
|--------------------------------------------------------------------------
|
| These functions must returns views that represent mails.
|
*/
/**
* @see SiteResetPasswordNotification
* @return HtmlString
*/
public function resetPasswordMail()
{
$customer = factory(SiteUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new SiteResetPasswordNotification($customer, $token));
}
/**
* @see KmsResetPasswordNotification
* @return HtmlString|View
*/
public function resetPasswordMailKms()
{
$customer = factory(KmsUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new KmsResetPasswordNotification($customer, $token));
}
/**
* @see CustomerRegisteredSetPassword
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function setPasswordMail()
{
$customer = factory(SiteUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new CustomerRegisteredSetPassword($customer, $token));
}
/**
* @see CustomerRegistered
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function registeredMail()
{
$customer = factory(SiteUser::class)->make();
return $this->notificationToView(new CustomerRegistered($customer));
}
/**
* @param string $status
* @param string $staffOrCustomer
* @param Order|null $order
* @return OrderStatusUpdatedCustomer|OrderStatusUpdatedStaff|HtmlString|View
*/
public function orderStatusMail(string $status, string $staffOrCustomer, Order $order = null)
{
$status = strtolower($status);
if(!OrderStatus::isValidItem(strtolower($status))) throw new \RuntimeException('The status must be one of the values of the enum '.OrderStatus::class);
if(!in_array($staffOrCustomer, ['staff', 'customer', true])) throw new \RuntimeException('The staffOrCustomer parameter must be "staff" or "customer" verbatim');
if(!in_array(strtolower($status), array_keys(__('shop/orders.mail'))) || __('shop/orders.mail.'.$status.'.'.$staffOrCustomer) === 'shop/orders.mail.'.$status.'.'.$staffOrCustomer)
throw new \RuntimeException('The translation file "shop/orders.mail.'.$status.'" does not have an entry for: '.$staffOrCustomer.'. It probable is not there by intention.');
/** @var Order $order */
if(!$order) $order = Order::inRandomOrder()->first();
$order->status = $status;
if(strtolower($staffOrCustomer) == 'customer') {
return new OrderStatusUpdatedCustomer($order);
} else {
return new OrderStatusUpdatedStaff($order, factory(KmsUser::class)->make());
}
}
/*
|--------------------------------------------------------------------------
| Invoice / Shipping notes previews
|--------------------------------------------------------------------------
|
| These functions must returns views that represent mails.
|
*/
/**
* @see InvoiceService@makeInvoiceForOrder
* @param Order|null $order
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function invoice(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
// return new OrderStatusUpdatedCustomer($order);
return new OrderStatusUpdatedStaff($order);
return $this->invoiceService->getInvoiceForOrderAsView($order);
}
/**
* @see InvoiceService@makeInvoiceForOrder
* @param Order|null $order
* @return
*/
public function invoicePdf(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return $this->invoiceService->getInvoiceForOrderAsPDF($order);
}
/**
* @see InvoiceService@makeInvoiceForOrder
* @param Order|null $order
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function invoicePdfDownload(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return $this->invoiceService->getInvoiceForOrderAsPDFDownload($order);
}
public function payLater(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return new OrderPayLaterCustomer($order);
}
/*
|--------------------------------------------------------------------------
| Authentication view previews
|--------------------------------------------------------------------------
|
| These functions must return blade views that are related to authentication
|
*/
public function registerView()
{
$culturesForSelect = RegionInfo::getSpecificCultures()->map(function(RegionInfo $regionInfo) {
return [$regionInfo->getName() => $regionInfo->getNativeName()];
})->collapse()->toArray();
return view('site.templates.auth.register', ['links' => $this->links, 'cultures' => $culturesForSelect]);
}
public function registeredView()
{
return view('site.templates.auth.registered', ['links' => $this->links]);
}
public function loginView()
{
return view('site.templates.auth.login', ['links' => $this->links]);
}
public function forgotPassword()
{
return view('site.templates.auth.password.email', ['links' => $this->links]);
}
public function set()
{
return view('site.templates.auth.password.set', ['token' => Str::random(32)]);
}
public function reset()
{
return view('site.templates.auth.password.reset', ['token' => Str::random(32)]);
}
public function loginViewKms()
{
return view('kms.auth.login', ['links' => $this->links]);
}
public function forgotPasswordKms()
{
return view('kms.auth.password.email', ['links' => $this->links]);
}
public function resetKms()
{
return view('kms.auth.password.reset', ['token' => Str::random(32)]);
}
public function resetedKms()
{
return view('kms.auth.password.reseted');
}
/*
|--------------------------------------------------------------------------
| Helper methods
|--------------------------------------------------------------------------
|
| These functions provide useful help for the ones that return views.
*/
/**
* Converts a notification to view
*
* @param Notification $notification
* @return HtmlString|View
*/
private function notificationToView(Notification $notification)
{
/** @var MailMessage $mail */
$mail = $notification->toMail(\Auth::user());
if($mail->markdown) {
$markdown = new Markdown(view());
return $markdown->render($mail->markdown, $mail->data());
}
elseif($mail->view)
{
return view($mail->view, $mail->data());
} else {
throw new \RuntimeException('Preview controller: I don\'t know how to render a view for "'.get_class($notification).'"');
}
}
public function invoiceMail(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return new OrderStatusUpdatedCustomer($order);
}
}