<?php
declare(strict_types=1);
namespace App\UI\Admin\Controller;
use App\Application\PaymentGatewayConfig\Command\EditPaymentGatewayConfig\EditPaymentGatewayConfigCommand;
use App\Application\Site\Command\ActivateSite\ActivateSiteCommand;
use App\Application\Site\Command\EditSite\EditSiteCommand;
use App\Application\Site\Command\EditSiteBillingInformation\EditSiteBillingInformationCommand;
use App\Application\Site\Command\RemoveSite\RemoveSiteCommand;
use App\Domain\PaymentGateway\Model\IbanXSConfiguration;
use App\Domain\PaymentGateway\Model\YapilyPayConfiguration;
use App\Domain\PaymentGateway\Model\DimocoPayConfiguration;
use App\Domain\Site\Model\Site;
use App\Infrastructure\Shared\Bus\Command\CommandBusInterface;
use App\UI\Admin\ActionStackFactory\SiteActionStackFactory;
use App\UI\Admin\Datatable\SiteDatatable;
use App\UI\Admin\Form\Type\PaymentGateway\PaymentGatewayConfigEditType;
use App\UI\Admin\Form\Type\Site\SiteCreateType;
use App\UI\Admin\Form\Type\Site\SiteEditBillingInformationType;
use App\UI\Admin\Form\Type\Site\SiteEditIntegrationDataType;
use App\UI\Admin\Form\Type\Site\SiteEditType;
use App\UI\Shared\Controller\AbstractController;
use App\UI\Shared\Status\BooleanStatus;
use Sg\DatatablesBundle\Response\DatatableResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
#[Route('/sites')]
class SiteController extends AbstractController
{
#[Route(path: '/', name: 'admin_sites')]
public function index(
Request $request,
SiteDatatable $datatable,
DatatableResponse $datatableResponse
): Response {
$isAjax = $request->isXmlHttpRequest();
$datatable->buildDatatable();
if ($isAjax) {
$datatableResponse->setDatatable($datatable);
$datatableResponse->getDatatableQueryBuilder();
return $datatableResponse->getResponse();
}
return $this->render('admin/site/index.html.twig', [
'datatable' => $datatable,
]);
}
#[Route(path: '/create', name: 'admin_sites_create')]
public function create(Request $request, CommandBusInterface $commandBus): Response
{
return $this->handleForm(
$request,
$commandBus,
SiteCreateType::class,
null,
[],
'admin/site/create.html.twig',
[],
'admin_sites_edit'
);
}
#[Route(path: '/{id}/edit', name: 'admin_sites_edit')]
public function edit(Request $request, Site $site, CommandBusInterface $commandBus): Response
{
return $this->handleForm(
$request,
$commandBus,
SiteEditType::class,
new EditSiteCommand(
(string) $site->getId(),
$site->getName(),
$site->getDescription(),
(string) $site->getMerchant()->getId()
),
[],
'admin/site/edit.html.twig',
[
'site' => $site,
],
);
}
#[Route(path: '/{id}/edit-billing-information', name: 'admin_sites_edit_billing_information')]
public function editBillingInformation(Request $request, Site $site, CommandBusInterface $commandBus): Response
{
return $this->handleForm(
$request,
$commandBus,
SiteEditBillingInformationType::class,
new EditSiteBillingInformationCommand(
(string) $site->getId(),
$site->getBillingInformation()->getName(),
$site->getBillingInformation()->getCountry(),
$site->getBillingInformation()->getCity(),
$site->getBillingInformation()->getStreet(),
$site->getBillingInformation()->getStreetNumber(),
$site->getBillingInformation()->getPostalCode(),
$site->getBillingInformation()->getIban(),
$site->getBillingInformation()->getIsBankWebhook()
),
[
'action' => $this->generateUrl('admin_sites_edit_billing_information', ['id' => $site->getId()]),
],
'admin/site/_edit_billing_information.html.twig',
[
'site' => $site,
],
);
}
#[Route(path: '/{id}/edit-payment-gateway-config', name: 'admin_sites_edit_payment_gateway_config')]
public function editPaymentGatewayConfig(
Request $request,
Site $site,
CommandBusInterface $commandBus,
SerializerInterface $serializer
): Response {
$paymentGatewayConfig = $site->getPaymentGatewayConfig();
$factoryName = $paymentGatewayConfig->getFactoryName();
$configArray = $paymentGatewayConfig->getConfig();
$config = null;
if (is_array($configArray) && count($configArray) > 0) {
// choose correct DTO/domain config class based on factory name
switch ($factoryName) {
case 'iban_xs':
$targetClass = IbanXSConfiguration::class;
break;
case 'yapily_pay':
$targetClass = YapilyPayConfiguration::class;
break;
case 'dimoco_pay':
$targetClass = DimocoPayConfiguration::class;
break;
default:
$targetClass = null;
}
if ($targetClass) {
$config = $serializer->deserialize(json_encode($configArray), $targetClass, 'json');
}
}
return $this->handleForm(
$request,
$commandBus,
PaymentGatewayConfigEditType::class,
new EditPaymentGatewayConfigCommand(
(string) $paymentGatewayConfig->getId(),
$paymentGatewayConfig->getFactoryName(),
$config
),
[
'action' => $this->generateUrl('admin_sites_edit_payment_gateway_config', ['id' => $site->getId()]),
],
'admin/site/_edit_payment_gateway_config.html.twig',
[
'site' => $site,
],
);
}
#[Route(path: '/{id}/edit-integration-data', name: 'admin_sites_edit_integration_data')]
public function editIntegrationData(Site $site): Response
{
$form = $this->createForm(SiteEditIntegrationDataType::class, [
'id' => $site->getId(),
'password' => $site->getPassword(),
'secret' => $site->getSecret(),
], [
'action' => $this->generateUrl('admin_sites_edit_integration_data', [
'id' => $site->getId(),
]),
]);
return $this->render('admin/site/_edit_integration_data.html.twig', [
'form' => $form->createView(),
'site' => $site,
]);
}
#[Route(path: '/{id}/status', name: 'admin_sites_status')]
public function status(
Site $site,
SiteActionStackFactory $siteActionStackFactory
): Response {
return $this->render('admin/site/status.html.twig', [
'site' => $site,
'status' => BooleanStatus::getHtmlStatus($site->isActive()),
'actionStack' => $siteActionStackFactory->create($site),
]);
}
#[Route(path: '/{id}/remove', name: 'admin_sites_remove')]
public function remove(Site $site, CommandBusInterface $commandBus): Response
{
try {
$commandBus->dispatch(new RemoveSiteCommand((string) $site->getId()));
} catch (\Exception $exception) {
return $this->createErrorJsonResponse($exception->getMessage());
}
return new Response(null, Response::HTTP_NO_CONTENT);
}
#[Route(path: '/{id}/restore', name: 'admin_sites_restore')]
public function restore(Site $site, CommandBusInterface $commandBus): Response
{
try {
$commandBus->dispatch(new ActivateSiteCommand((string) $site->getId()));
} catch (\Exception $exception) {
return $this->createErrorJsonResponse($exception->getMessage());
}
return new Response(null, Response::HTTP_NO_CONTENT);
}
}