/**
  * Create a new ship group for the address and dispatch events to add
  * destination, gifting and item data to the ship group.
  *
  * @param Mage_Customer_Model_Address_Abstract
  * @param Mage_Sales_Model_Order_Item[]
  * @param Mage_Sales_Model_Order
  * @param IShipGroupIterable
  * @param IOrderDestinationIterable
  * @param IOrderItemIterable
  * @return IShipGroup
  */
 protected function _buildShipGroupForAddress(Mage_Customer_Model_Address_Abstract $address, array $items, Mage_Sales_Model_Order $order, IShipGroupIterable $shipGroups, IOrderDestinationIterable $destinations, IOrderItemIterable $orderItems)
 {
     $shipGroup = $shipGroups->getEmptyShipGroup();
     // default set this value to flatrate shipping since magento doesn't
     // currently allow us to figure out how much each item contributes to
     // shipping. The value can be changed by responding to the following
     // event.
     $shipGroup->setChargeType(self::SHIPPING_CHARGE_TYPE_FLATRATE);
     Mage::dispatchEvent($this->_shipGroupEvent, ['address' => $address, 'order' => $order, 'ship_group_payload' => $shipGroup, 'order_destinations_payload' => $destinations]);
     // If none of the event observers added a destination, include a default
     // mapping of the address to a destination.
     if (is_null($shipGroup->getDestination())) {
         $shipGroup->setDestination($this->_buildDefaultDestination($address, $destinations));
     }
     return $this->_addOrderItemReferences($shipGroup, $items, $orderItems, $address, $order);
 }
 /**
  * Remove any ship groups that no longer contain items.
  *
  * @param IShipGroupIterable
  * @return IShipGroupIterable
  */
 protected function removeEmptyShipGroups(IShipGroupIterable $shipGroups, IOrderDestinationIterable $destinations)
 {
     // Can't loop over the iterable and modify it at the same time.
     // Collect items that need to be removed from the iterable but don't
     // modify the iterable.
     $emptyShipGroups = [];
     foreach ($shipGroups as $shipGroup) {
         if ($shipGroup->getItemReferences()->count() === 0) {
             $emptyShipGroups[] = $shipGroup;
         }
     }
     // Go through the items that need to be removed and remove each from
     // the iterable.
     foreach ($emptyShipGroups as $removeShipGroup) {
         // Destinations are stored separate from the ship group. Any destinations
         // for empty ship groups are no longer necessary so remove them with
         // the rest of the ship group.
         $destinations->offsetUnset($removeShipGroup->getDestination());
         $shipGroups->offsetUnset($removeShipGroup);
     }
     return $shipGroups;
 }