57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Model\Order;
|
|
|
|
use Magento\Framework\App\RequestInterface;
|
|
use Magento\Framework\App\State;
|
|
|
|
class CancellationSourceResolver
|
|
{
|
|
public const CUSTOMER = 'customer';
|
|
public const SCHEDULED_TASK = 'scheduled_task';
|
|
public const ADMINISTRATOR = 'administrator';
|
|
public const SYSTEM = 'system';
|
|
|
|
public function __construct(
|
|
private readonly State $appState,
|
|
private readonly RequestInterface $request
|
|
) {
|
|
}
|
|
|
|
public function resolve(): string
|
|
{
|
|
$areaCode = '';
|
|
try {
|
|
$areaCode = $this->appState->getAreaCode();
|
|
} catch (\Throwable) {
|
|
// Some programmatic cancellations run before an area is initialized.
|
|
}
|
|
if ($areaCode === 'crontab') {
|
|
return self::SCHEDULED_TASK;
|
|
}
|
|
|
|
$fullActionName = method_exists($this->request, 'getFullActionName')
|
|
? (string)$this->request->getFullActionName()
|
|
: '';
|
|
if ($fullActionName === 'esun_customer_order_cancel') {
|
|
return self::CUSTOMER;
|
|
}
|
|
if ($areaCode === 'adminhtml') {
|
|
return self::ADMINISTRATOR;
|
|
}
|
|
return self::SYSTEM;
|
|
}
|
|
|
|
public function getComment(string $source): string
|
|
{
|
|
return match ($source) {
|
|
self::CUSTOMER => 'The customer canceled this order from their account.',
|
|
self::SCHEDULED_TASK => 'A scheduled task canceled this order automatically.',
|
|
self::ADMINISTRATOR => 'An administrator canceled this order.',
|
|
default => 'A system process canceled this order.',
|
|
};
|
|
}
|
|
|
|
}
|