|
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\Core\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
15
|
|
|
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException; |
|
16
|
|
|
use Sylius\Component\Shipping\Model\ShipmentInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface as CoreShipmentInterface; |
|
18
|
|
|
use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface; |
|
19
|
|
|
use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface; |
|
20
|
|
|
use Webmozart\Assert\Assert; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class DefaultShippingMethodResolver implements DefaultShippingMethodResolverInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ShippingMethodRepositoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $shippingMethodRepository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param ShippingMethodRepositoryInterface $shippingMethodRepository |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(ShippingMethodRepositoryInterface $shippingMethodRepository) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->shippingMethodRepository = $shippingMethodRepository; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getDefaultShippingMethod(ShipmentInterface $shipment) |
|
44
|
|
|
{ |
|
45
|
|
|
Assert::isInstanceOf($shipment, CoreShipmentInterface::class); |
|
46
|
|
|
|
|
47
|
|
|
$shippingMethods = $this->shippingMethodRepository->findBy(['enabled' => true]); |
|
48
|
|
|
if (empty($shippingMethods)) { |
|
49
|
|
|
throw new UnresolvedDefaultShippingMethodException(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @var ChannelInterface $channel */ |
|
53
|
|
|
$channel = $shipment->getOrder()->getChannel(); |
|
54
|
|
|
|
|
55
|
|
|
foreach ($shippingMethods as $shippingMethod) { |
|
56
|
|
|
if ($channel->hasShippingMethod($shippingMethod)) { |
|
57
|
|
|
return $shippingMethod; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
throw new UnresolvedDefaultShippingMethodException(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|