Completed
Push — 1.2 ( 99ee81...7ed4f7 )
by Kamil
27s
created

DefaultShippingMethodResolver::getShippingMethods()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 25.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
declare(strict_types=1);
13
14
namespace Sylius\Component\Core\Resolver;
15
16
use Sylius\Component\Core\Model\ChannelInterface;
17
use Sylius\Component\Core\Model\ShipmentInterface as CoreShipmentInterface;
18
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
19
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException;
20
use Sylius\Component\Shipping\Model\ShipmentInterface;
21
use Sylius\Component\Shipping\Model\ShippingMethodInterface;
22
use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface;
23
use Webmozart\Assert\Assert;
24
25
@trigger_error('This class is deprecated since Sylius 1.2 and will be removed in 2.0. "'.sprintf("%s", EligibleDefaultShippingMethodResolver::class).'" should be used instead.', E_USER_DEPRECATED);
26
27
class DefaultShippingMethodResolver implements DefaultShippingMethodResolverInterface
28
{
29
    /**
30
     * @var ShippingMethodRepositoryInterface
31
     */
32
    private $shippingMethodRepository;
33
34
    /**
35
     * @param ShippingMethodRepositoryInterface $shippingMethodRepository
36
     */
37
    public function __construct(ShippingMethodRepositoryInterface $shippingMethodRepository)
38
    {
39
        $this->shippingMethodRepository = $shippingMethodRepository;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getDefaultShippingMethod(ShipmentInterface $shipment): ShippingMethodInterface
46
    {
47
        /** @var CoreShipmentInterface $shipment */
48
        Assert::isInstanceOf($shipment, CoreShipmentInterface::class);
49
50
        $order = $shipment->getOrder();
51
52
        /** @var ChannelInterface $channel */
53
        $channel = $order->getChannel();
54
55
        $shippingMethods = $this->shippingMethodRepository->findEnabledForChannel($channel);
56
        if (empty($shippingMethods)) {
57
            throw new UnresolvedDefaultShippingMethodException();
58
        }
59
60
        return $shippingMethods[0];
61
    }
62
}
63