fix: XML traversal, container autowire

This commit is contained in:
Vincent
2024-09-09 14:02:33 +02:00
parent 7861eaf5db
commit d932d735a3
2 changed files with 23 additions and 10 deletions

View File

@@ -12,8 +12,11 @@ use App\Entity\WatchListTrigger;
use App\Message\ProcessDomainTrigger;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository;
use App\Service\Connector\ConnectorInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
@@ -21,8 +24,6 @@ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use App\Service\Connector\ConnectorInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
#[AsMessageHandler]
final readonly class ProcessDomainTriggerHandler
@@ -36,7 +37,8 @@ final readonly class ProcessDomainTriggerHandler
private KernelInterface $kernel,
private LoggerInterface $logger,
private HttpClientInterface $client,
private ContainerBagInterface $container
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
) {
}
@@ -68,12 +70,14 @@ final readonly class ProcessDomainTriggerHandler
$connectorProviderClass = $provider->getConnectorProvider();
/** @var ConnectorInterface $connectorProvider */
$connectorProvider = $this->container->get($connectorProviderClass);
$connectorProvider = $this->locator->get($connectorProviderClass);
$connectorProvider->orderDomain($domain, $this->kernel->isDebug());
$connectorProvider->authenticate($connector->getAuthData());
$connectorProvider->orderDomain($domain, /* $this->kernel->isDebug() */ false);
$this->sendEmailDomainOrdered($domain, $connector, $watchList->getUser());
} catch (\Throwable $t) {
dump($t);
$this->logger->error('Unable to complete purchase. An error message is sent to user {username}.', [
'username' => $watchList->getUser()->getUserIdentifier(),
'error' => $t,

View File

@@ -3,8 +3,10 @@
namespace App\Service\Connector;
use App\Entity\Domain;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[Autoconfigure(public: true)]
class NamecheapConnector extends AbstractConnector
{
public const BASE_URL = 'https://api.namecheap.com/xml.response';
@@ -17,14 +19,19 @@ class NamecheapConnector extends AbstractConnector
public function orderDomain(Domain $domain, $dryRun): void
{
$addresses = $this->call('namecheap.users.address.getList', [], $dryRun);
$addressesRes = $this->call('namecheap.users.address.getList', [], $dryRun);
$addresses = $addressesRes->AddressGetListResult->List;
if (count($addresses) < 1) {
throw new \Exception('Namecheap account requires at least one address to purchase a domain');
}
$addressId = $addresses->{0}->AddressId;
$address = $this->call('namecheap.users.address.getinfo', ['AddressId' => $addressId], $dryRun);
$addressId = (string) $addresses->attributes()['AddressId'];
$address = (array) $this->call('namecheap.users.address.getinfo', ['AddressId' => $addressId], $dryRun)->GetAddressInfoResult;
if (empty($address['PostalCode'])) {
$address['PostalCode'] = $address['Zip'];
}
$domainAddresses = [];
@@ -33,6 +40,7 @@ class NamecheapConnector extends AbstractConnector
self::mergePrefixKeys('Admin', $address, $domainAddresses);
self::mergePrefixKeys('AuxBilling', $address, $domainAddresses);
$this->call('namecheap.domains.create', array_merge([
'DomainName' => $domain->getLdhName(),
'Years' => 1,
@@ -52,6 +60,7 @@ class NamecheapConnector extends AbstractConnector
{
$actualParams = array_merge([
'Command' => $command,
'UserName' => $this->authData['ApiUser'],
'ApiUser' => $this->authData['ApiUser'],
'ApiKey' => $this->authData['ApiKey'],
'ClientIp' => $this->outgoingIp,
@@ -63,8 +72,8 @@ class NamecheapConnector extends AbstractConnector
$data = new \SimpleXMLElement($response->getContent());
if ($data->errors->error) {
throw new \Exception(implode(', ', $data->errors->error)); // FIXME better exception type
if ($data->Errors->Error) {
throw new \Exception($data->Errors->Error); // FIXME better exception type
}
return $data->CommandResponse;