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

getDefaultShippingMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
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