PaymentMethodProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPaymentMethodsForOrder() 0 13 3
A isPaymentMethodAvailableForOrder() 0 12 3
1
<?php
2
3
namespace WellCommerce\Bundle\OrderBundle\Provider;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use WellCommerce\Bundle\OrderBundle\Entity\Order;
8
use WellCommerce\Bundle\OrderBundle\Entity\PaymentMethod;
9
use WellCommerce\Bundle\OrderBundle\Entity\ShippingMethod;
10
11
/**
12
 * Class PaymentMethodProvider
13
 *
14
 * @author  Adam Piotrowski <[email protected]>
15
 */
16
class PaymentMethodProvider implements PaymentMethodProviderInterface
17
{
18
    public function getPaymentMethodsForOrder(Order $order): Collection
19
    {
20
        $collection = new ArrayCollection();
21
        
22
        if ($order->getShippingMethod() instanceof ShippingMethod) {
23
            $collection = $order->getShippingMethod()->getPaymentMethods();
24
            $collection = $collection->filter(function (PaymentMethod $paymentMethod) use ($order) {
25
                return $paymentMethod->isEnabled() && $this->isPaymentMethodAvailableForOrder($paymentMethod, $order);
26
            });
27
        }
28
        
29
        return $collection;
30
    }
31
    
32
    private function isPaymentMethodAvailableForOrder(PaymentMethod $method, Order $order)
33
    {
34
        $client       = $order->getClient();
35
        $clientGroups = $method->getClientGroups();
36
        $clientGroup  = null !== $client ? $client->getClientGroup() : null;
37
        
38
        if ($clientGroups->count()) {
39
            return $clientGroups->contains($clientGroup);
40
        }
41
        
42
        return true;
43
    }
44
}
45