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

OrderClientDiscountVisitor::evaluateDiscount()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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