From 4e64212f4052a9e2f566f9fb2f4a1d076de8ab22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicolas=20B=C3=A9jean?= <nicolas@bejean.eu>
Date: Tue, 2 Mar 2021 22:21:36 +0100
Subject: [PATCH] feat: Create Helper

---
 Helper/SendGeoLocation.php | 81 ++++++++++++++++++++++++++++++++------
 1 file changed, 69 insertions(+), 12 deletions(-)

diff --git a/Helper/SendGeoLocation.php b/Helper/SendGeoLocation.php
index 7cdeb69..f530b13 100755
--- a/Helper/SendGeoLocation.php
+++ b/Helper/SendGeoLocation.php
@@ -1,10 +1,13 @@
 <?php
 namespace NicolasBejean\Customer\Helper;
 
-use Magento\Customer\Api\Data\AddressInterface;
-use Magento\Customer\Api\Data\CustomerInterface;
+use Magento\Customer\Model\Address;
+use Magento\Customer\Model\Customer;
 use Magento\Framework\App\Helper\AbstractHelper;
 use Magento\Framework\App\Helper\Context;
+use Magento\Framework\Exception\NoSuchEntityException;
+use Magento\Store\Api\WebsiteRepositoryInterface;
+use Magento\Directory\Api\CountryInformationAcquirerInterface;
 
 /**
  * Class SendGeoLocation
@@ -17,29 +20,83 @@ use Magento\Framework\App\Helper\Context;
  */
 class SendGeoLocation extends AbstractHelper
 {
+    /**
+     * @var WebsiteRepositoryInterface
+     */
+    private WebsiteRepositoryInterface $websiteRepositoryInterface;
+
+    /**
+     * @var CountryInformationAcquirerInterface
+     */
+    private CountryInformationAcquirerInterface $countryInformationAcquirerInterface;
+
     /**
      * SendGeoLocation constructor.
      * @param Context $context
+     * @param WebsiteRepositoryInterface $websiteRepositoryInterface
+     * @param CountryInformationAcquirerInterface $countryInformationAcquirerInterface
      */
     public function __construct(
-        Context $context
+        Context $context,
+        WebsiteRepositoryInterface $websiteRepositoryInterface,
+        CountryInformationAcquirerInterface $countryInformationAcquirerInterface
     ) {
+        $this->websiteRepositoryInterface = $websiteRepositoryInterface;
+        $this->countryInformationAcquirerInterface = $countryInformationAcquirerInterface;
+
         parent::__construct($context);
     }
 
-    public function sendDataToApi(CustomerInterface $customer, AddressInterface $address)
+    public function sendDataToApi(Customer $customer, Address $address)
     {
-        $createdAt = $customer->getCreatedAt();
-        $updatedAt = $customer->getUpdatedAt();
-
-        $websiteId = $customer->getWebsiteId();
-        $storeId = $customer->getStoreId();
-
         $customerId = $customer->getId();
-        $groupId = $customer->getGroupId();
+        $websiteId = $customer->getWebsiteId();
 
-        $country = $address->getCountryId();
+        $countryId = $address->getCountryId();
         $city = $address->getCity();
         $postCode = $address->getPostcode();
+
+        $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();
     }
 }
-- 
GitLab