Checks if addresses have been set and sets new ones

@param ApiOrderInterface $cart
7302
protected function validateOrCreateAddresses($cart)
    {
        if ($cart instanceof ApiOrderInterface) {
            $cart = $cart->getEntity();
        }
        if (!$cart->getDeliveryAddress() || !$cart->getInvoiceAddress()) {
            $addresses = $cart->getCustomerAccount()->getAccountAddresses();
            if ($addresses->isEmpty()) {
                throw new Exception('customer has no addresses');
            }
            $mainAddress = $cart->getCustomerAccount()->getMainAddress();
            if (!$mainAddress) {
                throw new Exception('customer has no main-address');
            }

            if (!$cart->getDeliveryAddress()) {
                $newAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddress(
                    $mainAddress,
                    $cart->getCustomerContact(),
                    $cart->getCustomerAccount()
                );
                $cart->setDeliveryAddress($newAddress);
                $this->em->persist($newAddress);
            }
            if (!$cart->getInvoiceAddress()) {
                $newAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddress(
                    $mainAddress,
                    $cart->getCustomerContact(),
                    $cart->getCustomerAccount()
                );
                $cart->setInvoiceAddress($newAddress);
                $this->em->persist($newAddress);
            }
        }
    }
674021
public function setAddress(array $address)
    {
        if (!empty($address) && is_array($address)) {
            $this->address = $address;
        }

        return $this;
    }
502372
public function moveItemsTo(Cart $cart){

        \DB::transaction(function () use(&$cart){
            $current_items = $cart->items()->pluck('product_id');
            $items_to_move = $this->items()->whereNotIn('product_id', $current_items->toArray())->get();
            if($items_to_move->count() === 0){
                return;
            }
            $this->items()->whereNotIn('product_id', $current_items->toArray())->update([
                'cart_id' => $cart->id
            ]);
            foreach($items_to_move as $item) {
                $this->item_count -= $item->quantity;
                $this->total_price -= $item->getPrice();
                $cart->item_count += $item->quantity;
                $cart->total_price += $item->getPrice();
            }
            $this->relations = [];
            $cart->relations = [];
            $this->save();
            $cart->save();
        });
        return $cart;
    }
644324
public function setSessionCart(Cart $cart = null)
    {
        if (null === $cart || $cart->isNew()) {
            self::$transientCart = $cart;
            $this->remove("thelia.cart_id");
        } else {
            self::$transientCart = null;
            $this->set("thelia.cart_id", $cart->getId());
        }

        return $this;
    }
838973
protected function setAddress(Address $address, $addressKey)
    {
        if (!$address->id) {
            $addressResource = $this->client
                ->addresses()
                ->create($address->asArray());

            if ($addressResource) {
                $address = $addressResource;
            }
        }

        $this->updateParams["{$addressKey}Id"] = $address->id;
    }
151367
public function removeAddress(AddressInterface $address)
    {
        if (!$this->hasAddress($address)) {
            throw new \InvalidArgumentException('Trying to remove an address that does not belong to this contact');
        }

        $this->addresses->removeElement($address);

        if ($address === $this->defaultAddress) {
            if ($this->addresses->count() > 0) {
                $this->defaultAddress = $this->addresses->first();

                return;
            }

            $this->defaultAddress = null;
        }
    }
200145
public function dispatchCartLineOnAddEvent(
        CartInterface $cart,
        CartLineInterface $cartLine
    ) {
        $this->eventDispatcher->dispatch(
            ElcodiCartEvents::CARTLINE_ONADD,
            new CartLineOnAddEvent(
                $cart,
                $cartLine
            )
        );

        return $this;
    }
107225
protected function setAddress(array $data)
    {
        if (empty($data['addresses'])) {
            return false;
        }

        foreach ($data['addresses'] as $address) {
            if (empty($address['address_id'])) {
                $address['user_id'] = $data['user_id'];
                $this->address->add($address);
            } else {
                $this->address->update($address['address_id'], $address);
            }
        }

        return true;
    }
806118
public function setAddress($address)
    {
        if (is_object($address)) {
            $this->address = $address;
        } elseif (is_string($address) || is_array($address)) {
            $this->address = new Address($address);
        }
    }
233946
public function onCheckout($cart)
    {
        if (!isset($this->creditCard))
            throw new CheckoutException('Credit Card is not set.', 0);

        if (!in_array($this->creditCard->getType(), $this->validTypes))
            throw new CheckoutException('Credit Card is not supported.', 1);

        if ($this->getPatternType($this->creditCard->getNumber()) != $this->creditCard->getType())
            throw new CheckoutException('Credit Card is invalid.', 2);
    }
485793
public function setAddress3($address3)
	{
		if ( $address3 == $this->getAddress3() ) { return; }

		$this->_values['address3'] = (string) $address3;
		$this->setModified();
	}
864707
protected function updateCart(CartInterface $cart, ClientInterface $client = null, $currency)
    {
        $needsUpdate = false;

        if ($client !== $cart->getClient()) {
            $cart->setClient($client);
            $needsUpdate = true;
        }

        if ($currency !== $cart->getCurrency()) {
            $cart->setCurrency($currency);
            $needsUpdate = true;
        }

        if ($needsUpdate) {
            $this->updateResource($cart);
        }
    }
511104
protected function setItemDeliveryAddress(
        $addressData,
        ApiItemInterface $item,
        ContactInterface $contact = null,
        AccountInterface $account = null
    ) {
        if ($item->getDeliveryAddress() === null) {
            // Create new delivery address.
            $deliveryAddress = new $this->orderAddressEntity();
            // Persist entities.
            $this->em->persist($deliveryAddress);
            // Assign to order.
            $item->setDeliveryAddress($deliveryAddress);
        }

        if (is_array($addressData)) {
            // Set order-address.
            $this->orderAddressManager->setOrderAddress(
                $item->getDeliveryAddress(),
                $addressData,
                $contact,
                $account
            );
        } elseif (is_int($addressData)) {
            $contactAddressId = $addressData;
            // Create order-address and assign contact-address data.
            $deliveryAddress = $item->getEntity()->getDeliveryAddress();

            $orderAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddressId(
                $contactAddressId,
                $contact,
                $account,
                $deliveryAddress
            );

            // Set delivery address.
            $item->setDeliveryAddress($orderAddress);

            // If new delivery address persist.
            if (!$deliveryAddress) {
                $this->em->persist($orderAddress);
            }
        }
    }
1159303
public function findCouponsByCart(CartInterface $cart)
    {
        $cartCoupons = $this
            ->createQueryBuilder('cc')
            ->where('cc.cart = :cart')
            ->setParameter('cart', $cart)
            ->getQuery()
            ->getResult();

        return array_map(function (CartCouponInterface $cartCoupon) {
            return $cartCoupon->getCoupon();
        }, $cartCoupons);
    }
1153238
public function updatePurchasableStockByCartLine(CartLineInterface $cartLine)
    {
        $purchasable = $cartLine->getPurchasable();
        $this
            ->purchasableStockUpdater
            ->updateStock(
                $purchasable,
                $cartLine->getQuantity()
            );
    }
130064
public function removeAddress(AddressInterface $address): void
    {
        $this->addresses->removeElement($address);

        if ($address->getId() == $this->defaultAddress->getId()) {
            if ($this->addresses->count() > 0) {
                $this->defaultAddress = $this->addresses[0];
            } else {
                $this->defaultAddress = null;
            }
        }
    }
1322871
protected function initAddress(array $address, $field)
    {
        $this->{$this->methodName('clear', $field)}();

        foreach ($address as $email => $label) {
            if (is_int($email)) {
                $email = $label;
            }

            $this->{$this->methodName('add', $field)}($email, $label);
        }
    }