Newer
Older
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
*
* @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
{
/**
* @var WebsiteRepositoryInterface
*/
private WebsiteRepositoryInterface $websiteRepositoryInterface;
/**
* @var CountryInformationAcquirerInterface
*/
private CountryInformationAcquirerInterface $countryInformationAcquirerInterface;
/**
* SendGeoLocation constructor.
* @param Context $context
* @param WebsiteRepositoryInterface $websiteRepositoryInterface
* @param CountryInformationAcquirerInterface $countryInformationAcquirerInterface
Context $context,
WebsiteRepositoryInterface $websiteRepositoryInterface,
CountryInformationAcquirerInterface $countryInformationAcquirerInterface
$this->websiteRepositoryInterface = $websiteRepositoryInterface;
$this->countryInformationAcquirerInterface = $countryInformationAcquirerInterface;
public function sendDataToApi(Customer $customer, Address $address)
$city = $address->getCity();
$postCode = $address->getPostcode();
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$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();