Completed
Push — master ( a54a4b...bbfdca )
by Adam
15:54
created

MinimumAmountVisitor::visitOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\OrderBundle\Visitor;
14
15
use WellCommerce\Bundle\AppBundle\Entity\Client;
16
use WellCommerce\Bundle\AppBundle\Entity\ClientGroup;
17
use WellCommerce\Bundle\AppBundle\Entity\MinimumOrderAmount;
18
use WellCommerce\Bundle\AppBundle\Helper\CurrencyHelperInterface;
19
use WellCommerce\Bundle\OrderBundle\Entity\Order;
20
21
/**
22
 * Class MinimumAmountVisitor
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
class MinimumAmountVisitor implements OrderVisitorInterface
27
{
28
    /**
29
     * @var CurrencyHelperInterface
30
     */
31
    private $currencyHelper;
32
    
33
    public function __construct(CurrencyHelperInterface $currencyHelper)
34
    {
35
        $this->currencyHelper = $currencyHelper;
36
    }
37
    
38
    public function visitOrder(Order $order)
39
    {
40
        $minimumOrderAmount = $this->getMinimumOrderAmount($order);
41
        $baseCurrency       = strlen($minimumOrderAmount->getCurrency()) ? $minimumOrderAmount->getCurrency() : null;
42
        $targetCurrency     = $order->getCurrency();
43
        $value              = $this->currencyHelper->convert($minimumOrderAmount->getValue(), $baseCurrency, $targetCurrency);
44
        
45
        $order->getMinimumOrderAmount()->setCurrency($targetCurrency);
46
        $order->getMinimumOrderAmount()->setValue($value);
47
    }
48
    
49
    private function getMinimumOrderAmount(Order $order): MinimumOrderAmount
50
    {
51
        $client = $order->getClient();
52
        
53
        if ($client instanceof Client) {
54
            if ($client->getMinimumOrderAmount()->getValue() > 0) {
55
                return $client->getMinimumOrderAmount();
56
            }
57
            
58
            $clientGroup = $client->getClientGroup();
59
            
60
            if ($clientGroup instanceof ClientGroup) {
61
                if ($clientGroup->getMinimumOrderAmount()->getValue() > 0) {
62
                    return $clientGroup->getMinimumOrderAmount();
63
                }
64
            }
65
        }
66
        
67
        return $order->getShop()->getMinimumOrderAmount();
68
    }
69
}
70