<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */
namespace Eadesigndev\RomCity\Model\Quote;

use Eadesigndev\RomCity\Api\Quote\ShippingMethodManagementInterface;
use Eadesigndev\RomCity\Api\Quote\Data\EstimateAddressInterface;
use Magento\Quote\Model\ShippingMethodManagement as MagentoShippingMethodManagement;

/**
 * Shipping method read service.
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
#[\AllowDynamicProperties]
class ShippingMethodManagement extends MagentoShippingMethodManagement implements ShippingMethodManagementInterface
{
    /**
     * {@inheritDoc}
     */
    public function estimateRatesByAddress($cartId, EstimateAddressInterface $address)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->getActive($cartId);

        // no methods applicable for empty carts or carts with virtual products
        if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
            return [];
        }

        $quote->getShippingAddress()->setCity($address->getCity());

        return $this->getEstimatedRates(
            $quote,
            $address->getCountryId(),
            $address->getPostcode(),
            $address->getRegionId(),
            $address->getRegion()
        );
    }

    /**
     * {@inheritDoc}
     */
    public function estimateRatesByAddressId($cartId, $addressId)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->getActive($cartId);

        // no methods applicable for empty carts or carts with virtual products
        if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
            return [];
        }
        $address = $this->addressRepository->getById($addressId);

        $quote->getShippingAddress()->setCity($address->getCity());

        return $this->getEstimatedRates(
            $quote,
            $address->getCountryId(),
            $address->getPostcode(),
            $address->getRegionId(),
            $address->getRegion()
        );
    }
}