Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't rely on Laravel config component #154

Merged
merged 5 commits into from
Aug 30, 2023
Merged
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
36 changes: 18 additions & 18 deletions src/VatCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Config\Repository;
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException;
use Mpociot\VatCalculator\Util\ConfigWrapper;
use SoapClient;
use SoapFault;

Expand Down Expand Up @@ -521,7 +522,7 @@ class VatCalculator
protected $postalCode = '';

/**
* @var Repository
* @var array
*/
protected $config;

Expand Down Expand Up @@ -558,16 +559,14 @@ class VatCalculator
protected $ukValidationEndpoint = 'https://api.service.hmrc.gov.uk';

/**
* @param \Illuminate\Contracts\Config\Repository
* @param \Illuminate\Contracts\Config\Repository|array
*/
public function __construct($config = null)
public function __construct($config = [])
{
$this->config = $config;

$businessCountryKey = 'vat_calculator.business_country_code';
$this->config = $config instanceof Repository ? $config->get('vat_calculator', []) : $config;

if (isset($this->config) && $this->config->has($businessCountryKey)) {
$this->setBusinessCountryCode($this->config->get($businessCountryKey, ''));
if (isset($this->config['business_country_code'])) {
$this->setBusinessCountryCode($this->config['business_country_code']);
}
}

Expand All @@ -579,9 +578,9 @@ public function __construct($config = null)
*/
public function shouldCollectVAT($countryCode)
{
$taxKey = 'vat_calculator.rules.'.strtoupper($countryCode);
$countryCode = strtoupper($countryCode);

return isset($this->taxRules[strtoupper($countryCode)]) || (isset($this->config) && $this->config->has($taxKey));
return isset($this->taxRules[$countryCode]) || isset($this->config['rules'][$countryCode]);
}

/**
Expand Down Expand Up @@ -749,15 +748,16 @@ public function getTaxRateForCountry($countryCode, $company = false, $type = nul
*/
public function getTaxRateForLocation($countryCode, $postalCode = null, $company = false, $type = null)
{
if ($company && strtoupper($countryCode) !== strtoupper($this->businessCountryCode)) {
$countryCode = strtoupper($countryCode);

if ($company && $countryCode !== strtoupper($this->businessCountryCode)) {
return 0;
}

$taxRules = $this->taxRules;
$taxKey = 'vat_calculator.rules.'.strtoupper($countryCode);

if (isset($this->config) && $this->config->has($taxKey)) {
$configTax = $this->config->get($taxKey, 0);
if (isset($this->config['rules'][$countryCode])) {
$configTax = $this->config['rules'][$countryCode];

if (is_array($configTax)) {
$taxRules[$countryCode] = $configTax;
Expand Down Expand Up @@ -876,7 +876,7 @@ public function getVATDetails($vatNumber)
'vatNumber' => $vatNumber,
]);
} catch (SoapFault $e) {
if (isset($this->config) && $this->config->get('vat_calculator.forward_soap_faults')) {
if ($this->config['forward_soap_faults'] ?? false) {
throw new VATCheckUnavailableException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

Expand All @@ -902,16 +902,16 @@ public function initSoapClient()
// Set's default timeout time.
$timeout = 30;

if (isset($this->config) && $this->config->has('vat_calculator.soap_timeout')) {
$timeout = $this->config->get('vat_calculator.soap_timeout');
if (isset($this->config['soap_timeout'])) {
$timeout = $this->config['soap_timeout'];
}

$context = stream_context_create(['http' => ['timeout' => $timeout]]);

try {
$this->soapClient = new SoapClient(self::VAT_SERVICE_URL, ['stream_context' => $context]);
} catch (SoapFault $e) {
if (isset($this->config) && $this->config->get('vat_calculator.forward_soap_faults')) {
if ($this->config['forward_soap_faults'] ?? false) {
throw new VATCheckUnavailableException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

Expand Down
Loading