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

PaymentMethodProvider::getPaymentMethodsForOrder()   A

Complexity

Conditions 3
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 13
Code Lines 7

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
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