Completed
Pull Request — master (#197)
by Mateusz
55:00
created

OrderShipmentProcessor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 80
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B process() 0 27 6
A getOrderShipment() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Processor;
6
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\ShipmentInterface;
9
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
10
use Sylius\Component\Order\Processor\OrderProcessorInterface;
11
use Sylius\Component\Resource\Factory\FactoryInterface;
12
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException;
13
use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface;
14
use Webmozart\Assert\Assert;
15
16
/**
17
 * @author Paweł Jędrzejewski <[email protected]>
18
 */
19
final class OrderShipmentProcessor implements OrderProcessorInterface
20
{
21
    /**
22
     * @var DefaultShippingMethodResolverInterface
23
     */
24
    private $defaultShippingMethodResolver;
25
26
    /**
27
     * @var FactoryInterface
28
     */
29
    private $shipmentFactory;
30
31
    /**
32
     * @param DefaultShippingMethodResolverInterface $defaultShippingMethodResolver
33
     * @param FactoryInterface $shipmentFactory
34
     */
35
    public function __construct(
36
        DefaultShippingMethodResolverInterface $defaultShippingMethodResolver,
37
        FactoryInterface $shipmentFactory
38
    ) {
39
        $this->defaultShippingMethodResolver = $defaultShippingMethodResolver;
40
        $this->shipmentFactory = $shipmentFactory;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function process(BaseOrderInterface $order)
47
    {
48
        /** @var OrderInterface $order */
49
        Assert::isInstanceOf($order, OrderInterface::class);
50
51
        if ($order->isEmpty()) {
52
            $order->removeShipments();
53
54
            return;
55
        }
56
57
        $shipment = $this->getOrderShipment($order);
58
59
        if (null === $shipment) {
60
            return;
61
        }
62
63
        foreach ($shipment->getUnits() as $unit) {
64
            $shipment->removeUnit($unit);
65
        }
66
67
        foreach ($order->getItemUnits() as $itemUnit) {
68
            if (null === $itemUnit->getShipment()) {
69
                $shipment->addUnit($itemUnit);
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param BaseOrderInterface $order
76
     *
77
     * @return ShipmentInterface
78
     */
79
    private function getOrderShipment(BaseOrderInterface $order)
80
    {
81
        if ($order->hasShipments()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\Model\OrderInterface as the method hasShipments() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\Order.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
82
            return $order->getShipments()->first();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\Model\OrderInterface as the method getShipments() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\Order.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
83
        }
84
85
        try {
86
            /** @var ShipmentInterface $shipment */
87
            $shipment = $this->shipmentFactory->createNew();
88
            $shipment->setOrder($order);
89
            $shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
90
91
            $order->addShipment($shipment);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\Model\OrderInterface as the method addShipment() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\Order.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
92
93
            return $shipment;
94
        } catch (UnresolvedDefaultShippingMethodException $exception) {
95
            return null;
96
        }
97
    }
98
}
99