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
|
|
|
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
|
|
|
|
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.