| 1 |
<?php |
| 2 |
|
| 3 |
namespace Packetery\Module\Shipping; |
| 4 |
|
| 5 |
use Packetery\Module\Carrier\Downloader; |
| 6 |
use Packetery\Module\Carrier\PacketaPickupPointsConfig; |
| 7 |
use Packetery\Nette\PhpGenerator\PhpFile; |
| 8 |
|
| 9 |
class ShippingMethodGenerator { |
| 10 |
private const TARGET_NAMESPACE = 'Packetery\Module\Shipping\Generated'; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var PacketaPickupPointsConfig |
| 14 |
*/ |
| 15 |
private $pickupPointConfig; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var Downloader |
| 19 |
*/ |
| 20 |
private $carrierDownloader; |
| 21 |
|
| 22 |
public function __construct( |
| 23 |
PacketaPickupPointsConfig $pickupPointConfig, |
| 24 |
Downloader $carrierDownloader |
| 25 |
) { |
| 26 |
$this->pickupPointConfig = $pickupPointConfig; |
| 27 |
$this->carrierDownloader = $carrierDownloader; |
| 28 |
} |
| 29 |
|
| 30 |
public function generateClasses(): void { |
| 31 |
$allCarriers = []; |
| 32 |
|
| 33 |
$pickupPointCarriers = array_merge( $this->pickupPointConfig->getCompoundCarriers(), $this->pickupPointConfig->getVendorCarriers() ); |
| 34 |
foreach ( $pickupPointCarriers as $pickupPointCarrier ) { |
| 35 |
$allCarriers[ $pickupPointCarrier->getId() ] = $pickupPointCarrier->getName(); |
| 36 |
} |
| 37 |
|
| 38 |
$feedCarriers = $this->carrierDownloader->fetch_as_array( 'en' ); |
| 39 |
foreach ( $feedCarriers as $feedCarrier ) { |
| 40 |
$allCarriers[ $feedCarrier['id'] ] = $feedCarrier['name']; |
| 41 |
} |
| 42 |
|
| 43 |
if ( count( $allCarriers ) === 0 ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
foreach ( $allCarriers as $carrierId => $carrierName ) { |
| 48 |
if ( ! self::classExists( $carrierId ) ) { |
| 49 |
$this->generateClass( $carrierId, $carrierName ); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @link https://doc.nette.org/en/php-generator |
| 56 |
*/ |
| 57 |
private function generateClass( string $carrierId, string $carrierName ): void { |
| 58 |
$file = new PhpFile(); |
| 59 |
$file->addComment( 'This file is auto-generated.' ); |
| 60 |
$file->setStrictTypes(); |
| 61 |
|
| 62 |
$namespace = $file->addNamespace( self::TARGET_NAMESPACE ); |
| 63 |
$namespace->addUse( BaseShippingMethod::class ); |
| 64 |
|
| 65 |
$className = self::getClassnameFromCarrierId( $carrierId ); |
| 66 |
$class = $namespace->addClass( $className ); |
| 67 |
$class->setExtends( BaseShippingMethod::class )->addComment( $carrierName ); |
| 68 |
|
| 69 |
// Add setType later. |
| 70 |
$class->addConstant( 'CARRIER_ID', $carrierId )->setPublic(); |
| 71 |
|
| 72 |
$filePath = __DIR__ . '/Generated/' . $className . '.php'; |
| 73 |
|
| 74 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 75 |
file_put_contents( $filePath, $file ); |
| 76 |
} |
| 77 |
|
| 78 |
private static function getClassnameFromCarrierId( string $carrierId ): string { |
| 79 |
return 'ShippingMethod_' . $carrierId; |
| 80 |
} |
| 81 |
|
| 82 |
public static function classExists( string $carrierId ): bool { |
| 83 |
return class_exists( self::TARGET_NAMESPACE . '\\' . self::getClassnameFromCarrierId( $carrierId ) ); |
| 84 |
} |
| 85 |
} |
| 86 |
|