Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
SendCustomerData.php 2.37 KiB
Newer Older
Nicolas's avatar
Nicolas committed
<?php
namespace NicolasBejean\Customer\Observer;

Nicolas's avatar
Nicolas committed
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\Data\CustomerInterface;
Nicolas's avatar
Nicolas committed
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
Nicolas's avatar
Nicolas committed
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
Nicolas's avatar
Nicolas committed
use NicolasBejean\Customer\Helper\SendGeoLocation;
Nicolas's avatar
Nicolas committed
use Psr\Log\LoggerInterface;
Nicolas's avatar
Nicolas committed
 * Class SendCustomerData
Nicolas's avatar
Nicolas committed
 *
 * @category PHP
 * @package  NicolasBejean\Customer\Observer
 * @author   Nicolas Béjean <nicolas@bejean.eu>
 * @license  https://lab.frogg.it/bejean-developpement/magento-2/modules/customer/-/blob/master/LICENCE GPL3 Licence
 * @link     https://www.bejean.eu
 */
Nicolas's avatar
Nicolas committed
class SendCustomerData implements ObserverInterface
Nicolas's avatar
Nicolas committed
{
Nicolas's avatar
Nicolas committed
    /**
     * @var AccountManagementInterface
     */
    private AccountManagementInterface $accountManagement;

    /**
     * @var LoggerInterface
     */
    private LoggerInterface $logger;

    /**
Nicolas's avatar
Nicolas committed
     * @var SendGeoLocation
     */
    private SendGeoLocation $helper;

    /**
     * SendCustomerData constructor.
Nicolas's avatar
Nicolas committed
     *
     * @param AccountManagementInterface $accountManagement
     * @param LoggerInterface $logger
Nicolas's avatar
Nicolas committed
     * @param SendGeoLocation $helper
Nicolas's avatar
Nicolas committed
     */
    public function __construct(
        AccountManagementInterface $accountManagement,
Nicolas's avatar
Nicolas committed
        LoggerInterface $logger,
        SendGeoLocation $helper
Nicolas's avatar
Nicolas committed
    ) {
        $this->accountManagement = $accountManagement;
        $this->logger = $logger;
Nicolas's avatar
Nicolas committed
        $this->helper = $helper;
Nicolas's avatar
Nicolas committed
    }
Nicolas's avatar
Nicolas committed

    /**
     * Execute method
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        $event = $observer->getEvent();

        if ($event->getName() === 'customer_register_success') {
Nicolas's avatar
Nicolas committed
            /** @var CustomerInterface $customer */
            $customer = $observer->getData('customer');

            try {
                $defaultBillingAddress = $this->accountManagement->getDefaultBillingAddress($customer->getId());
            } catch (NoSuchEntityException | LocalizedException $e) {
                $this->logger->error(__('An error has occurred while retrieving the default billing address.'));
                exit();
            }

            if (!is_null($defaultBillingAddress)) {
Nicolas's avatar
Nicolas committed
                $this->helper->sendDataToApi($customer, $defaultBillingAddress);
Nicolas's avatar
Nicolas committed
            }
Nicolas's avatar
Nicolas committed
            exit();