Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
17 / 17
TotalAmountStrategy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
17 / 17
 supports(OrderInterface $order)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
2 / 2
 autocomplete( OrderInterface $order, ConfigurationInterface $configuration )
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 getProductsCost( ProductCollectionInterface $productCollection )
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
9 / 9
<?php
/**
 * @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl>
 */
namespace Team3\PayU\Order\Autocomplete\Strategy;
use Team3\PayU\Configuration\ConfigurationInterface;
use Team3\PayU\Order\Model\Money\Money;
use Team3\PayU\Order\Model\Money\MoneyInterface;
use Team3\PayU\Order\Model\OrderInterface;
use Team3\PayU\Order\Model\Products\ProductCollectionInterface;
use Team3\PayU\Order\Model\Products\ProductInterface;
class TotalAmountStrategy implements AutocompleteStrategyInterface
{
    /**
     * @param OrderInterface $order
     *
     * @return bool
     */
    public function supports(OrderInterface $order)
    {
        return 0 == $order->getTotalAmount()->getValue()
            && 0 < $order->getProductCollection()->count();
    }
    /**
     * @param OrderInterface         $order
     * @param ConfigurationInterface $configuration
     */
    public function autocomplete(
        OrderInterface $order,
        ConfigurationInterface $configuration
    ) {
        $order->setTotalAmount(
            $this->getProductsCost(
                $order->getProductCollection()
            )
        );
    }
    /**
     * @param ProductCollectionInterface $productCollection
     *
     * @return MoneyInterface
     */
    private function getProductsCost(
        ProductCollectionInterface $productCollection
    ) {
        $totalAmount = new Money(0);
        /** @var ProductInterface $product */
        foreach ($productCollection as $product) {
            // $totalAmount += $unitPrice * $quantity
            $totalAmount = $totalAmount->add(
                $product->getUnitPrice()->multiply(
                    $product->getQuantity()
                )
            );
        }
        return $totalAmount;
    }
}