Completed
Push — master ( b31cd6...60f5be )
by Kamil
43:45
created

DefaultShippingMethodResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultShippingMethod() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Shipping\Resolver;
13
14
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException;
15
use Sylius\Component\Shipping\Model\ShipmentInterface;
16
use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface;
17
18
/**
19
 * @author Mateusz Zalewski <[email protected]>
20
 */
21
class DefaultShippingMethodResolver implements DefaultShippingMethodResolverInterface
22
{
23
    /**
24
     * @var ShippingMethodRepositoryInterface
25
     */
26
    private $shippingMethodRepository;
27
28
    /**
29
     * @param ShippingMethodRepositoryInterface $shippingMethodRepository
30
     */
31
    public function __construct(ShippingMethodRepositoryInterface $shippingMethodRepository)
32
    {
33
        $this->shippingMethodRepository = $shippingMethodRepository;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getDefaultShippingMethod(ShipmentInterface $shipment)
40
    {
41
        $shippingMethods = $this->shippingMethodRepository->findBy(['enabled' => true]);
42
        if (empty($shippingMethods)) {
43
            throw new UnresolvedDefaultShippingMethodException();
44
        }
45
        
46
        return $shippingMethods[0];
47
    }
48
}
49