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
SendGeoLocation.php 3.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas's avatar
    Nicolas committed
    <?php
    namespace NicolasBejean\Customer\Helper;
    
    
    Nicolas's avatar
    Nicolas committed
    use Magento\Customer\Model\Address;
    use Magento\Customer\Model\Customer;
    
    Nicolas's avatar
    Nicolas committed
    use Magento\Framework\App\Helper\AbstractHelper;
    use Magento\Framework\App\Helper\Context;
    
    Nicolas's avatar
    Nicolas committed
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Store\Api\WebsiteRepositoryInterface;
    use Magento\Directory\Api\CountryInformationAcquirerInterface;
    
    Nicolas's avatar
    Nicolas committed
    
    /**
     * Class SendGeoLocation
     *
     * @category PHP
     * @package  NicolasBejean\Customer\Helper
     * @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
     */
    class SendGeoLocation extends AbstractHelper
    {
    
    Nicolas's avatar
    Nicolas committed
        /**
         * @var WebsiteRepositoryInterface
         */
        private WebsiteRepositoryInterface $websiteRepositoryInterface;
    
        /**
         * @var CountryInformationAcquirerInterface
         */
        private CountryInformationAcquirerInterface $countryInformationAcquirerInterface;
    
    
    Nicolas's avatar
    Nicolas committed
        /**
         * SendGeoLocation constructor.
         * @param Context $context
    
    Nicolas's avatar
    Nicolas committed
         * @param WebsiteRepositoryInterface $websiteRepositoryInterface
         * @param CountryInformationAcquirerInterface $countryInformationAcquirerInterface
    
    Nicolas's avatar
    Nicolas committed
         */
        public function __construct(
    
    Nicolas's avatar
    Nicolas committed
            Context $context,
            WebsiteRepositoryInterface $websiteRepositoryInterface,
            CountryInformationAcquirerInterface $countryInformationAcquirerInterface
    
    Nicolas's avatar
    Nicolas committed
        ) {
    
    Nicolas's avatar
    Nicolas committed
            $this->websiteRepositoryInterface = $websiteRepositoryInterface;
            $this->countryInformationAcquirerInterface = $countryInformationAcquirerInterface;
    
    
    Nicolas's avatar
    Nicolas committed
            parent::__construct($context);
        }
    
    
    Nicolas's avatar
    Nicolas committed
        public function sendDataToApi(Customer $customer, Address $address)
    
    Nicolas's avatar
    Nicolas committed
        {
            $customerId = $customer->getId();
    
    Nicolas's avatar
    Nicolas committed
            $websiteId = $customer->getWebsiteId();
    
    Nicolas's avatar
    Nicolas committed
    
    
    Nicolas's avatar
    Nicolas committed
            $countryId = $address->getCountryId();
    
    Nicolas's avatar
    Nicolas committed
            $city = $address->getCity();
            $postCode = $address->getPostcode();
    
    Nicolas's avatar
    Nicolas committed
    
            $websiteName = $this->getWebsiteNameFromId($websiteId);
            $countryName = $this->getCountryNameFromId($countryId);
    
            $data = array(
                'customerId' => $customerId,
                'websiteName' => $websiteName,
                'countryName' => $countryName,
                'postCode' => $postCode,
                'city' => $city
            );
    
            /* Instructions */
        }
    
        /**
         * @param int $websiteId
         * @return string
         */
        private function getWebsiteNameFromId (int $websiteId): string
        {
            try {
                $website = $this->websiteRepositoryInterface->getById($websiteId);
            } catch (NoSuchEntityException $e) {
                return '';
            }
    
            return $website->getName();
        }
    
        /**
         * @param string $countryId
         * @return string
         */
        private function getCountryNameFromId (string $countryId): string
        {
            try {
                $country = $this->countryInformationAcquirerInterface->getCountryInfo($countryId);
            } catch (NoSuchEntityException $e) {
                return '';
            }
    
            return $country->getFullNameLocale();
    
    Nicolas's avatar
    Nicolas committed
        }
    }