interface DeliveryInterface
{
public const TYPE_COURIER = 1;
public const TYPE_PICKUP_POINT = 2;
public const TYPE_ALL = self::TYPE_COURIER + self::TYPE_PICKUP_POINT;
public function getId(): int;
public function getName(): string;
public function getType(): int;
public function getPaymentTypes(): array;
public function isAllowedAddress(Address $address): bool;
public function calculateCost(Address $from, Address $to): array;
}
interface PickupDeliveryInterface extends DeliveryInterface
{
public function getPickupPoints(Address $address = null): array;
}
class DeliveryManager
{
private $deliveries = [];
public function registerDelivery(DeliveryInterface $delivery)
{
$this->deliveries[$delivery->getId()] = $delivery;
}
public function registerDeliveryClass(int $id, string $className, array $config = [])
{
$this->deliveries[$id] = [$className, ['id' => $id] + $config];
}
public function getDelivery(int $id): DeliveryInterface
{
if (!isset($this->deliveries[$id])) {
throw new InvalidArgumentException();
}
if (is_array($this->deliveries[$id])) {
[$className, $config] = $this->deliveries[$id];
$this->deliveries[$id] = new $className($config);
}
return $this->deliveries[$id];
}
}