|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Walmart |
| 5 | + * PHP version 7.4 |
| 6 | + * |
| 7 | + * @category Class |
| 8 | + * @package {{invokerPackage}} |
| 9 | + * @author Jesse Evers |
| 10 | + * @link https://highsidelabs.co |
| 11 | + */ |
| 12 | + |
| 13 | +{{>partials/partial_header}} |
| 14 | + |
| 15 | +namespace {{invokerPackage}}; |
| 16 | + |
| 17 | +use BadMethodCallException; |
| 18 | +use RuntimeException; |
| 19 | +use {{apiPackage}}\CP\ContentProviderApi; |
| 20 | +use {{apiPackage}}\DSV\DropShipVendorApi; |
| 21 | +use {{apiPackage}}\MP\MarketplaceApi; |
| 22 | +use {{apiPackage}}\WS\WarehouseSupplierApi; |
| 23 | +use {{invokerPackage}}\Configuration; |
| 24 | + |
| 25 | +abstract class Walmart |
| 26 | +{ |
| 27 | + public const VERSION = '{{artifactVersion}}'; |
| 28 | + |
| 29 | + /** |
| 30 | + * The configuration for this client. |
| 31 | + * @var Configuration |
| 32 | + */ |
| 33 | + protected Configuration $config; |
| 34 | + |
| 35 | + /** |
| 36 | + * The country to API class map. This should be overwritten by every child class. |
| 37 | + * @var array<string, string[]> |
| 38 | + */ |
| 39 | + protected static array $countryApiMap; |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a new Walmart client. |
| 43 | + * |
| 44 | + * @param Configuration $config The configuration for this client. |
| 45 | + * @param ?string $country The country to use for this client. |
| 46 | + */ |
| 47 | + public function __construct(Configuration $config) |
| 48 | + { |
| 49 | + $this->config = $config; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Enable calling country-specific APIs as single methods on this class, without needing to |
| 54 | + * specify the country outside $this->config or manually instantiate the API class. |
| 55 | + * |
| 56 | + * @param mixed $name The name of the method being called. |
| 57 | + * @param mixed $arguments The arguments passed to the callee. |
| 58 | + * @throws BadMethodCallException |
| 59 | + * @throws RuntimeException |
| 60 | + * @return mixed |
| 61 | + */ |
| 62 | + public function __call($name, $arguments) |
| 63 | + { |
| 64 | + // Don't override existing methods |
| 65 | + if (method_exists(get_class($this), $name)) { |
| 66 | + return $this->$name(...$arguments); |
| 67 | + } |
| 68 | + |
| 69 | + if (!array_key_exists($name, static::$countryApiMap)) { |
| 70 | + throw new BadMethodCallException("Method $name does not exist"); |
| 71 | + } |
| 72 | + |
| 73 | + if (!array_key_exists($this->config->getCountry(), static::$countryApiMap[$name])) { |
| 74 | + $supportedCountriesStr = implode(', ', array_keys(static::$countryApiMap[$name])); |
| 75 | + throw new RuntimeException( |
| 76 | + "$name API is not supported in country {$this->config->getCountry()}. Supported country(ies): $supportedCountriesStr" |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + $apiClass = static::$countryApiMap[$name][$this->config->getCountry()]; |
| 81 | + return new $apiClass($this->config); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return an instance of the Content Provider API provider. |
| 86 | + * |
| 87 | + * @param Configuration $config |
| 88 | + * @return ContentProviderApi |
| 89 | + */ |
| 90 | + public static function contentProvider(Configuration $config): ContentProviderApi |
| 91 | + { |
| 92 | + return new ContentProviderApi($config); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Return an instance of the Drop Ship Vendor API provider. |
| 97 | + * |
| 98 | + * @param Configuration $config |
| 99 | + * @return DropShipVendorApi |
| 100 | + */ |
| 101 | + public static function dropShipVendor(Configuration $config): DropShipVendorApi |
| 102 | + { |
| 103 | + return new DropShipVendorApi($config); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Return an instance of the Marketplace API provider. |
| 108 | + * |
| 109 | + * @param Configuration $config |
| 110 | + * @return MarketplaceApi |
| 111 | + */ |
| 112 | + public static function marketplace(Configuration $config): MarketplaceApi |
| 113 | + { |
| 114 | + return new MarketplaceApi($config); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Return an instance of the Warehouse Supplier API provider. |
| 119 | + * |
| 120 | + * @param Configuration $config |
| 121 | + * @return WarehouseSupplierApi |
| 122 | + */ |
| 123 | + public static function warehouseSupplier(Configuration $config): WarehouseSupplierApi |
| 124 | + { |
| 125 | + return new WarehouseSupplierApi($config); |
| 126 | + } |
| 127 | +} |
0 commit comments