Skip to content
Open

3.7.0 #119

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.gitattributes export-ignore
.gitignore export-ignore
.github/ export-ignore
54 changes: 43 additions & 11 deletions Block/System/Config/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public function __construct(
$this->_dataHelper = $dataHelper;
$this->_updateNotifier = $updateNotifier;

$this->_cachedData = $this->_getCachedData();

$this->_notifyUpdate();

parent::__construct($context, $data);
}

Expand All @@ -85,6 +81,9 @@ public function __construct(
*/
public function render(AbstractElement $element): string
{
$this->_cachedData = $this->_getCachedData();
$this->_notifyUpdate();

/** @noinspection PhpUndefinedMethodInspection */
$this->setElement($element);

Expand All @@ -101,7 +100,7 @@ public function getConfig(): array
return [
'enabled' => $this->_storeConfigHelper->isEnabled(),
'module_version' => $this->_storeConfigHelper->getModuleVersion(),
'supported_countries' => $this->_storeConfigHelper->getSupportedCountries(),
'supported_countries' => $this->_storeConfigHelper->getSupportedCountryNames(),
'account_name' => $this->_storeConfigHelper->getValue('account_name'),
'account_status' => $this->_storeConfigHelper->getValue('account_status'), // Defaults to "new", see etc/config.xml.
'has_credentials' => $this->_storeConfigHelper->hasCredentials(),
Expand Down Expand Up @@ -138,19 +137,52 @@ public function getApiStatusDescription(): string
$status = $this->_storeConfigHelper->getValue('account_status');

switch ($status) {
case \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_NEW:
return __('not connected');
case \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE:
case ApiClientHelper::API_ACCOUNT_STATUS_NEW:
return __('new');
case ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE:
return __('active');
case \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_INVALID_CREDENTIALS:
case ApiClientHelper::API_ACCOUNT_STATUS_INVALID_CREDENTIALS:
return __('invalid key and/or secret');
case \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_INACTIVE:
case ApiClientHelper::API_ACCOUNT_STATUS_INACTIVE:
return __('inactive');
default:
throw new Status\Exception(__('Invalid account status value.'));
}
}

/**
* Get hint about API status.
*
* @return string
*/
public function getApiStatusHint(): string
{
$status = $this->_storeConfigHelper->getValue('account_status');

switch ($status) {
case ApiClientHelper::API_ACCOUNT_STATUS_NEW:
return __('Enter your Postcode.eu API key and secret to connect.');
case ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE:
return __('The Postcode.eu API is successfully connected.');
case ApiClientHelper::API_ACCOUNT_STATUS_INVALID_CREDENTIALS:
return __('The API key or secret is incorrect. Please check your credentials.');
case ApiClientHelper::API_ACCOUNT_STATUS_INACTIVE:
return __('Your Postcode.eu subscription is inactive. Please log in to your account to resolve this.');
default:
throw new Status\Exception(__('Invalid account status value.'));
}
}

/**
* Check if API status is active.
*
* @return bool
*/
public function isStatusActive(): bool
{
return $this->_storeConfigHelper->getValue('account_status') === ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE;
}

/**
* Get cached data.
*
Expand Down Expand Up @@ -180,7 +212,7 @@ private function _getCachedData(): array
private function _getAccountInfo(): array
{
$status = $this->_storeConfigHelper->getValue('account_status');
if ($status === \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE) {
if ($status === ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE) {
return $this->_apiClientHelper->getAccountInfo();
}

Expand Down
145 changes: 145 additions & 0 deletions Data/Form/Element/AddressAutofill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Flekto\Postcode\Data\Form\Element;

use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\Form\Element\CollectionFactory;
use Magento\Framework\Data\Form\Element\Factory;
use Magento\Framework\Escaper;
use Magento\Framework\View\LayoutInterface;

/**
* Add custom element for address autocomplete controls
*/
class AddressAutofill extends AbstractElement
{
/** @var LayoutInterface */
protected $_layout;

/**
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param LayoutInterface $layout
* @param array $data
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
LayoutInterface $layout,
$data = []
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
$this->setType('postcode-eu-address-autofill');
$this->_layout = $layout;
}

/**
* Get element HTML
*
* @return string
*/
public function getElementHtml(): string
{
$block = $this->_layout->createBlock(
\Magento\Backend\Block\Template::class,
$this->getId(),
[
'data' => [
'template' => 'Flekto_Postcode::form/element/address-autofill.phtml',
'jsLayout' => $this->getJsLayout(),
],
],
);

return $block->toHtml();
}

/**
* Get JS layout
*
* @return array
*/
public function getJsLayout(): array
{
return [
'components' => [
$this->getId() => [
'component' => 'Flekto_Postcode/js/view/address/autofill',
'config' => [
'settings' => $this->getData('settings'),
'htmlIdPrefix' => $this->getData('htmlIdPrefix'),
'addressType' => $this->getData('addressType'),
'countryCode' => $this->getData('countryCode'),
'visible' => $this->getData('visible'),
],
'children' => [
'address_autofill_nl' => [
'component' => 'Flekto_Postcode/js/view/form/sales/order_create/address-autofill-nl',
'config' => [
'componentDisabled' => $this->getData('isNlComponentDisabled'),
],
'children' => [
'postcode' => [
'component' => 'Magento_Ui/js/form/element/abstract',
'label' => __('Zip/Postal Code'),
'config' => [
'template' => 'ui/form/field',
'elementTmpl' => 'Flekto_Postcode/form/element/address-autofill-field',
'placeholder' => '1234 AB',
'imports' => [
'visible' => '${ $.parentName }:visible',
],
],
],
'house_number' => [
'component' => 'Magento_Ui/js/form/element/abstract',
'label' => __('House number and addition'),
'additionalClasses' => [
'address-autofill-nl-house-number' => true,
],
'config' => [
'template' => 'ui/form/field',
'elementTmpl' => 'Flekto_Postcode/form/element/address-autofill-field',
'imports' => [
'visible' => '${ $.parentName }:visible',
],
],
],
'house_number_select' => [
'component' => 'Magento_Ui/js/form/element/select',
'label' => __('Which house number do you mean?'),
'config' => [
'caption' => __('- Select house number -'),
'template' => 'ui/form/field',
'visible' => false,
],
],
],
],
'address_autofill_intl' => [
'component' => 'Flekto_Postcode/js/view/form/sales/order_create/address-autofill-intl',
'label' => __('Find an address'),
'placeholder' => __('City, street or postcode')
],
'address_autofill_error' => [
'component' => 'Magento_Ui/js/form/components/html',
'config' => [
'visible' => false,
'listens' => [
'${$.parentName}:error' => 'content',
'content' => 'visible',
],
'additionalClasses' => [
'admin__field-note' => true,
'address-autofill-warning' => true,
],
],
],
],
],
],
];
}
}
5 changes: 5 additions & 0 deletions HTTP/Client/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
*/
class Curl extends \Magento\Framework\HTTP\Client\Curl
{
/**
* Request timeout
* @var int type
*/
protected $_timeout = 30;
}
24 changes: 20 additions & 4 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\HTTP\Client\Curl;
use Flekto\Postcode\HTTP\Client\Curl;
use Flekto\Postcode\Service\Exception\CurlException;

class Data extends AbstractHelper
{
Expand Down Expand Up @@ -166,7 +167,12 @@ private function _getPackageData(): array
}
}

$this->_curl->get(self::PACKAGIST_URL);
try {
$this->_curl->get(self::PACKAGIST_URL);
} catch (CurlException $e) {
throw new LocalizedException(__('Failed to fetch package data: %1', $e->getMessage()));
}

$status = $this->_curl->getStatus();
if ($status == 200) {
$response = $this->_curl->getBody();
Expand All @@ -175,7 +181,12 @@ private function _getPackageData(): array
throw new LocalizedException(__('Failed to write package data to %1.', $filePath));
}

return json_decode($response, true);
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new LocalizedException(__('Invalid JSON response from Packagist.'));
}

return $result;

} elseif ($status == 304) { // Not modified, use cached file.
$data = $this->_fs->fileGetContents($filePath);
Expand All @@ -184,7 +195,12 @@ private function _getPackageData(): array
throw new LocalizedException(__('Failed to read package data from %1.', $filePath));
}

return json_decode($data, true);
$result = json_decode($data, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new LocalizedException(__('Invalid cached JSON data.'));
}

return $result;
}

throw new LocalizedException(__('Unexpected status code %1 while fetching package data.', $status));
Expand Down
31 changes: 29 additions & 2 deletions Helper/StoreConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Magento\Store\Model\StoreManagerInterface;
use Magento\Developer\Helper\Data as DeveloperHelperData;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
use Magento\Framework\Locale\ResolverInterface;
use Flekto\Postcode\Model\Config\Source\NlInputBehavior;
use Flekto\Postcode\Model\Config\Source\ShowHideAddressFields;

Expand All @@ -16,6 +18,8 @@ class StoreConfigHelper extends AbstractHelper
protected $_storeManager;
protected $_developerHelper;
protected $_encryptor;
protected $_countryCollectionFactory;
protected $_localeResolver;

public const PATH = [
// Status
Expand All @@ -38,23 +42,30 @@ class StoreConfigHelper extends AbstractHelper
'disabled_countries' => 'postcodenl_api/advanced_config/disabled_countries',
'allow_pobox_shipping' => 'postcodenl_api/advanced_config/allow_pobox_shipping',
'split_street_values' => 'postcodenl_api/advanced_config/split_street_values',
'admin_address_autocomplete_behavior' => 'postcodenl_api/advanced_config/admin_address_autocomplete_behavior',
];

/**
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param Data $developerHelper
* @param DeveloperHelperData $developerHelper
* @param EncryptorInterface $encryptor
* @param CountryCollectionFactory $countryCollectionFactory
* @param ResolverInterface $localeResolver
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
DeveloperHelperData $developerHelper,
EncryptorInterface $encryptor
EncryptorInterface $encryptor,
CountryCollectionFactory $countryCollectionFactory,
ResolverInterface $localeResolver
) {
$this->_storeManager = $storeManager;
$this->_developerHelper = $developerHelper;
$this->_encryptor = $encryptor;
$this->_countryCollectionFactory = $countryCollectionFactory;
$this->_localeResolver = $localeResolver;
parent::__construct($context);
}

Expand Down Expand Up @@ -122,6 +133,22 @@ public function getEnabledCountries(): array
return array_values(array_diff($supported, explode(',', $disabled)));
}

/**
* Get supported country names.
*
* @return array
*/
public function getSupportedCountryNames(): array
{
$isoCodes = array_map(fn($country) => $country->iso2, $this->getSupportedCountries());
$collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['in' => $isoCodes]);
$locale = $this->_localeResolver->getLocale();
$names = array_map(fn($country) => $country->getName(), $collection->getItems());
\Collator::create($locale)->asort($names);

return $names;
}

/**
* Check if API credentials are set.
*
Expand Down
Loading