AbstractDhl.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Dhl\Model;
  7. use Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
  8. abstract class AbstractDhl extends AbstractCarrierOnline
  9. {
  10. /**
  11. * Response condition code for service is unavailable at the requested date
  12. */
  13. const CONDITION_CODE_SERVICE_DATE_UNAVAILABLE = 1003;
  14. /**
  15. * Count of days to look forward if day is not unavailable
  16. */
  17. const UNAVAILABLE_DATE_LOOK_FORWARD = 5;
  18. /**
  19. * Date format for request
  20. */
  21. const REQUEST_DATE_FORMAT = 'Y-m-d';
  22. /**
  23. * Get shipping date
  24. *
  25. * @return string
  26. */
  27. protected function _getShipDate()
  28. {
  29. return $this->_determineShippingDay($this->getConfigData('shipment_days'), date(self::REQUEST_DATE_FORMAT));
  30. }
  31. /**
  32. * Determine shipping day according to configuration settings
  33. *
  34. * @param string[] $shippingDays
  35. * @param string $date
  36. * @return string
  37. */
  38. protected function _determineShippingDay($shippingDays, $date)
  39. {
  40. if (empty($shippingDays)) {
  41. return $date;
  42. }
  43. $shippingDays = explode(',', $shippingDays);
  44. $i = -1;
  45. do {
  46. $i++;
  47. $weekday = date('D', strtotime("{$date} +{$i} day"));
  48. } while (!in_array($weekday, $shippingDays) && $i < 10);
  49. return date(self::REQUEST_DATE_FORMAT, strtotime("{$date} +{$i} day"));
  50. }
  51. }