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

getDiscountForClientGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
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 Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use WellCommerce\Bundle\AppBundle\Entity\Client;
17
use WellCommerce\Bundle\AppBundle\Helper\CurrencyHelperInterface;
18
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractContainerAware;
19
use WellCommerce\Bundle\OrderBundle\Entity\Order;
20
use WellCommerce\Bundle\OrderBundle\Provider\OrderModifierProviderInterface;
21
22
/**
23
 * Class OrderClientDiscountVisitor
24
 *
25
 * @author  Adam Piotrowski <[email protected]>
26
 */
27
class OrderClientGroupDiscountVisitor extends AbstractContainerAware implements OrderVisitorInterface
28
{
29
    /**
30
     * @var OrderModifierProviderInterface
31
     */
32
    private $orderModifierProvider;
33
    
34
    /**
35
     * @var CurrencyHelperInterface
36
     */
37
    private $currencyHelper;
38
    
39
    /**
40
     * OrderClientDiscountVisitor constructor.
41
     *
42
     * @param OrderModifierProviderInterface $orderModifierProvider
43
     * @param CurrencyHelperInterface        $currencyHelper
44
     */
45
    public function __construct(OrderModifierProviderInterface $orderModifierProvider, CurrencyHelperInterface $currencyHelper)
46
    {
47
        $this->orderModifierProvider = $orderModifierProvider;
48
        $this->currencyHelper        = $currencyHelper;
49
    }
50
    
51
    public function visitOrder(Order $order)
52
    {
53
        $modifierValue = 0;
54
        
55
        if (null === $order->getCoupon()) {
56
            $discount      = $this->evaluateDiscount($order);
57
            $modifierValue = round($discount / 100, 2);
58
        }
59
        
60
        if ($modifierValue > 0) {
61
            $modifier = $this->orderModifierProvider->getOrderModifier($order, 'client_group_discount');
62
            $modifier->setCurrency($order->getCurrency());
63
            $modifier->setGrossAmount($order->getProductTotal()->getGrossPrice() * $modifierValue);
64
            $modifier->setNetAmount($order->getProductTotal()->getNetPrice() * $modifierValue);
65
            $modifier->setTaxAmount($order->getProductTotal()->getTaxAmount() * $modifierValue);
66
            
67
        } else {
68
            $order->removeModifier('client_group_discount');
69
        }
70
    }
71
    
72
    private function evaluateDiscount(Order $order): float
73
    {
74
        $client = $order->getClient();
75
        
76
        if ($client instanceof Client) {
77
            $clientGroup = $client->getClientGroup();
78
            $language    = new ExpressionLanguage();
79
            
80
            try {
81
                $expression = $clientGroup->getDiscount();
82
                
83
                return (float)$language->evaluate($expression, [
84
                    'order' => $order,
85
                ]);
86
            } catch (\Exception $exception) {
87
                return 0;
88
            }
89
            
90
        }
91
        
92
        return 0;
93
    }
94
}
95