Completed
Push — 1.9 ( 30c3e5...ca8b96 )
by
unknown
42:25
created

isProcessingAllowed()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 12
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Strategy;
4
5
use OroCRM\Bundle\MagentoBundle\Entity\MagentoSoapTransport;
6
use OroCRM\Bundle\MagentoBundle\Entity\Order;
7
use OroCRM\Bundle\MagentoBundle\ImportExport\Converter\GuestCustomerDataConverter;
8
use OroCRM\Bundle\MagentoBundle\Provider\Reader\ContextCartReader;
9
use OroCRM\Bundle\MagentoBundle\Provider\Reader\ContextCustomerReader;
10
11
class OrderWithExistingCustomerStrategy extends OrderStrategy
12
{
13
    const CONTEXT_ORDER_POST_PROCESS = 'postProcessOrders';
14
15
    /**
16
     * @param Order $importingOrder
17
     *
18
     * {@inheritdoc}
19
     */
20 View Code Duplication
    public function process($importingOrder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        if (!$this->isProcessingAllowed($importingOrder)) {
23
            $this->appendDataToContext(self::CONTEXT_ORDER_POST_PROCESS, $this->context->getValue('itemData'));
24
25
            return null;
26
        }
27
28
        return parent::process($importingOrder);
29
    }
30
31
    /**
32
     * @param Order $order
33
     * @return bool
34
     */
35
    protected function isProcessingAllowed(Order $order)
36
    {
37
        $isProcessingAllowed = true;
38
        $customer = $this->findExistingEntity($order->getCustomer());
39
        $customerOriginId = $order->getCustomer()->getOriginId();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Oro\Bundle\BusinessEntit...undle\Entity\BasePerson as the method getOriginId() does only exist in the following sub-classes of Oro\Bundle\BusinessEntit...undle\Entity\BasePerson: OroCRM\Bundle\MagentoBundle\Entity\Customer, OroCRM\Bundle\MagentoBun...s\Entity\ExtendCustomer. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
40
        if (!$customer && $customerOriginId) {
41
            $this->appendDataToContext(ContextCustomerReader::CONTEXT_POST_PROCESS_CUSTOMERS, $customerOriginId);
42
43
            $isProcessingAllowed = false;
44
        }
45
46
        // Do not try to load cart if bridge does not installed
47
        /** @var MagentoSoapTransport $transport */
48
        $channel = $this->databaseHelper->findOneByIdentity($order->getChannel());
49
        $transport = $channel->getTransport();
50
        if ($transport->getIsExtensionInstalled()) {
51
            $cart = $this->findExistingEntity($order->getCart());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $cart is correct as $this->findExistingEntity($order->getCart()) (which targets OroCRM\Bundle\MagentoBun...y::findExistingEntity()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
            $cartOriginId = $order->getCart()->getOriginId();
53
            if (!$cart && $cartOriginId) {
54
                $this->appendDataToContext(ContextCartReader::CONTEXT_POST_PROCESS_CARTS, $cartOriginId);
55
56
                $isProcessingAllowed = false;
57
            }
58
        }
59
60
        if (!$customer && $order->getIsGuest() && $transport->getGuestCustomerSync()) {
61
            $this->appendDataToContext(
62
                'postProcessGuestCustomers',
63
                GuestCustomerDataConverter::extractCustomersValues((array)$this->context->getValue('itemData'))
64
            );
65
66
            $isProcessingAllowed = false;
67
        }
68
69
        return $isProcessingAllowed;
70
    }
71
}
72