File: D:/HostingSpaces/SBogers10/beat-the-barn.komma.nl/app/Development/PreviewController.php
<?php
namespace App\Development;
use App\Components\ComponentTypes;
use App\Pages\PageService;
use Faker\Generator;
use Illuminate\Routing\Controller;
use Komma\KMS\Components\Component\ViewComponent;
use Komma\KMS\Notifications\KmsSetPasswordNotification;
use Komma\KMS\Users\Models\KmsUser;
use Komma\KMS\Notifications\KmsResetPasswordNotification;
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 \stdClass
*/
private $links;
public function __construct()
{
if(app()->runningInConsole()) return;
$pageService = new PageService();
$this->links = $pageService->getAllTranslatedPageRoutes();
}
/*
|--------------------------------------------------------------------------
| Mail previews
|--------------------------------------------------------------------------
|
| These functions must returns views that represent mails.
|
*/
/**
* @see KmsResetPasswordNotification
* @return HtmlString|View
*/
public function resetPasswordMailKms()
{
$user = factory(KmsUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new KmsResetPasswordNotification($user, $token));
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @see SetPassword
*/
public function setPasswordMailKms()
{
$customer = factory(KmsUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new KmsSetPasswordNotification($customer, $token));
}
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)]);
}
/*
|--------------------------------------------------------------------------
| Components
|--------------------------------------------------------------------------
|
| These functions must return views that display components
|
*/
public function components()
{
$faker = app(Generator::class);
$components = collect(ComponentTypes::all())->mapWithKeys(function(int $componentTypeId) use($faker) {
$regularComponent = $faker->componentOfType($componentTypeId);
return [__('KMS::attributes/components.types.'.$componentTypeId). ' (ComponentTypes::'.array_search($componentTypeId, ComponentTypes::getAsArray()).')' => new ViewComponent($regularComponent)];
});
return view('templates.components_preview')->with([
"components" => $components,
"links" => $this->links
]);
}
/*
|--------------------------------------------------------------------------
| 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).'"');
}
}
}