Skip to content

Commit

Permalink
Merge pull request #2 from umizoomi/master
Browse files Browse the repository at this point in the history
Compatibility update with the latest postcode-nl/api-magento2-module version
  • Loading branch information
experius-nl authored Jan 5, 2023
2 parents 37a396d + b9234ed commit 3f597be
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
10 changes: 5 additions & 5 deletions Model/Resolver/AddressDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace Experius\PostcodeGraphQl\Model\Resolver;

use Experius\Core\Helper\Settings;
use Flekto\Postcode\Helper\PostcodeApiClient;
use Flekto\Postcode\Helper\StoreConfigHelper;
use Flekto\Postcode\Service\PostcodeApiClient;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand All @@ -29,11 +29,11 @@ class AddressDetails implements ResolverInterface
* @param Settings $helper
*/
public function __construct(
Settings $helper
StoreConfigHelper $helper
) {
$this->postcodeHelper = new PostcodeApiClient(
$helper->getConfigValue('postcodenl_api/general/api_key'),
$helper->getConfigValue('postcodenl_api/general/api_secret')
$helper->getValue(StoreConfigHelper::PATH['api_key']),
$helper->getValue(StoreConfigHelper::PATH['api_secret'])
);
}

Expand Down
41 changes: 34 additions & 7 deletions Model/Resolver/Autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@

namespace Experius\PostcodeGraphQl\Model\Resolver;

use Experius\Core\Helper\Settings;
use Flekto\Postcode\Helper\CountryCodeConvertorHelper;
use Flekto\Postcode\Helper\PostcodeApiClient;
use Flekto\Postcode\Helper\StoreConfigHelper;
use Flekto\Postcode\Service\PostcodeApiClient;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Directory\Api\CountryInformationAcquirerInterface;
use Magento\Directory\Api\Data\CountryInformationInterface;

class Autocomplete implements ResolverInterface
{
Expand All @@ -28,17 +29,24 @@ class Autocomplete implements ResolverInterface
*/
protected $postcodeHelper;

/**
* @var CountryInformationAcquirerInterface
*/
protected $countryInterface;

/**
* PostcodeManagement constructor.
* @param Settings $helper
*/
public function __construct(
Settings $helper
StoreConfigHelper $helper,
CountryInformationAcquirerInterface $countryInterface
) {
$this->postcodeHelper = new PostcodeApiClient(
$helper->getConfigValue('postcodenl_api/general/api_key'),
$helper->getConfigValue('postcodenl_api/general/api_secret')
$helper->getValue(StoreConfigHelper::PATH['api_key']),
$helper->getValue(StoreConfigHelper::PATH['api_secret'])
);
$this->countryInterface = $countryInterface;
}

/**
Expand All @@ -57,7 +65,7 @@ public function resolve(
if (!isset($args['searchTerm']) || !$args['searchTerm']) {
throw new GraphQlInputException(__('"searchTerm" should be specified'));
}
$countryId = CountryCodeConvertorHelper::alpha2ToAlpha3($args['countryId']);
$countryId = $this->iso2ToIso3Code($args['countryId']);
if(!isset($args['xAutocompleteSession'])) {
$session = bin2hex(random_bytes(8));
} else {
Expand All @@ -77,4 +85,23 @@ public function resolve(
}
return $result;
}

/**
* @param string $countryId
* @return string
*/
private function iso2ToIso3Code($countryId)
{
try {
/**
* @var CountryInformationInterface $countryInformation
*/
$countryInformation = $this->countryInterface->getCountryInfo($countryId);

return strtolower($countryInformation->getThreeLetterAbbreviation());
} catch (\Exception $e) {

}
return $countryId;
}
}
12 changes: 6 additions & 6 deletions Model/Resolver/Postcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

namespace Experius\PostcodeGraphQl\Model\Resolver;

use Experius\Core\Helper\Settings;
use Flekto\Postcode\Helper\Exception\ClientException;
use Flekto\Postcode\Helper\PostcodeApiClient;
use Flekto\Postcode\Helper\StoreConfigHelper;
use Flekto\Postcode\Service\Exception\ClientException;
use Flekto\Postcode\Service\PostcodeApiClient;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand All @@ -33,11 +33,11 @@ class Postcode implements ResolverInterface
* @param PostcodeApiClient $postcodeHelper
*/
public function __construct(
Settings $helper
StoreConfigHelper $helper
) {
$this->postcodeHelper = new PostcodeApiClient(
$helper->getConfigValue('postcodenl_api/general/api_key'),
$helper->getConfigValue('postcodenl_api/general/api_secret')
$helper->getValue(StoreConfigHelper::PATH['api_key']),
$helper->getValue(StoreConfigHelper::PATH['api_secret'])
);
}

Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Experius_PostcodeGraphQl">
<sequence>
<module name="Experius_Postcode"/>
<module name="Flekto_Postcode"/>
<module name="Magento_GraphQl"/>
</sequence>
</module>
Expand Down

0 comments on commit 3f597be

Please sign in to comment.