95 lines
3.4 KiB
PHP
95 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Agent\Controller\Index;
|
|
|
|
use Esun\Agent\Model\ApplicationFactory;
|
|
use Magento\Framework\App\Action\HttpPostActionInterface;
|
|
use Magento\Framework\App\RequestInterface;
|
|
use Magento\Framework\Controller\Result\Json;
|
|
use Magento\Framework\Controller\Result\JsonFactory;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class Apply implements HttpPostActionInterface
|
|
{
|
|
public function __construct(
|
|
private readonly RequestInterface $request,
|
|
private readonly JsonFactory $jsonFactory,
|
|
private readonly ApplicationFactory $applicationFactory,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
public function execute(): Json
|
|
{
|
|
$result = $this->jsonFactory->create();
|
|
|
|
try {
|
|
$data = $this->validateRequest();
|
|
$this->applicationFactory->create()
|
|
->setData($data)
|
|
->save();
|
|
|
|
return $result->setData([
|
|
'success' => true,
|
|
'message' => (string)__('Thank you. Your application has been submitted successfully.'),
|
|
]);
|
|
} catch (LocalizedException $exception) {
|
|
$result->setHttpResponseCode(422);
|
|
return $result->setData([
|
|
'success' => false,
|
|
'message' => $exception->getMessage(),
|
|
]);
|
|
} catch (\Throwable $exception) {
|
|
$this->logger->critical($exception);
|
|
$result->setHttpResponseCode(500);
|
|
return $result->setData([
|
|
'success' => false,
|
|
'message' => (string)__('Unable to submit your application. Please try again later.'),
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function validateRequest(): array
|
|
{
|
|
if (trim((string)$this->request->getParam('hideit', '')) !== '') {
|
|
throw new LocalizedException(__('Unable to submit your application.'));
|
|
}
|
|
|
|
$data = [
|
|
'company_name' => trim((string)$this->request->getParam('company_name', '')),
|
|
'contact_person' => trim((string)$this->request->getParam('contact_person', '')),
|
|
'country' => trim((string)$this->request->getParam('country', '')),
|
|
'email' => trim((string)$this->request->getParam('email', '')),
|
|
'message' => trim((string)$this->request->getParam('message', '')),
|
|
];
|
|
|
|
if ($data['company_name'] === '') {
|
|
throw new LocalizedException(__('Enter the company name.'));
|
|
}
|
|
if ($data['contact_person'] === '') {
|
|
throw new LocalizedException(__('Enter the contact person.'));
|
|
}
|
|
if ($data['country'] === '') {
|
|
throw new LocalizedException(__('Select a country.'));
|
|
}
|
|
if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
|
|
throw new LocalizedException(__('Enter a valid email address.'));
|
|
}
|
|
if (
|
|
mb_strlen($data['company_name']) > 255 ||
|
|
mb_strlen($data['contact_person']) > 255 ||
|
|
mb_strlen($data['country']) > 255 ||
|
|
mb_strlen($data['email']) > 255
|
|
) {
|
|
throw new LocalizedException(__('One or more fields are too long.'));
|
|
}
|
|
if (mb_strlen($data['message']) > 5000) {
|
|
throw new LocalizedException(__('The message must not exceed 5000 characters.'));
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|