-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Create TaxClasses by TaxClassFactory
Relates: #505
- Loading branch information
Showing
5 changed files
with
106 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Extcode\Cart\Domain\Model\Cart; | ||
|
||
/* | ||
* This file is part of the package extcode/cart. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
final class TaxClassFactory implements TaxClassFactoryInterface | ||
{ | ||
public function __construct( | ||
private readonly LoggerInterface $logger | ||
) {} | ||
|
||
public function getTaxClass(int $taxClassKey, array $taxClassValue): ?TaxClass | ||
{ | ||
if ($this->isValidTaxClassConfig($taxClassKey, $taxClassValue)) { | ||
return new TaxClass( | ||
$taxClassKey, | ||
$taxClassValue['value'], | ||
(float)$taxClassValue['calc'], | ||
$taxClassValue['name'] | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function isValidTaxClassConfig(int $key, array $value): bool | ||
{ | ||
if (empty($value) || | ||
empty($value['name']) || | ||
!isset($value['calc']) || | ||
!is_numeric($value['calc']) | ||
) { | ||
$this->logger->error('Can\'t create tax class object for the configuration with the index=' . $key . '.', []); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Extcode\Cart\Domain\Model\Cart; | ||
|
||
/* | ||
* This file is part of the package extcode/cart. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
interface TaxClassFactoryInterface | ||
{ | ||
public function getTaxClass(int $taxClassKey, array $taxClassValue): ?TaxClass; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Extcode\Cart\Domain\Model\Cart; | ||
|
||
/* | ||
* This file is part of the package extcode/cart. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
interface TaxClassInterface | ||
{ | ||
public function getId(): int; | ||
|
||
public function getValue(): string; | ||
|
||
public function getCalc(): float; | ||
|
||
public function getTitle(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters