Completed
Pull Request — master (#5)
by Tim
02:51
created

EeProductObserver::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Ee\Observers\EeProductObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-ee
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Ee\Observers;
22
23
use TechDivision\Import\Product\Ee\Utils\MemberNames;
24
use TechDivision\Import\Product\Observers\ProductObserver;
25
26
/**
27
 * Observer that create's the product itself for the Magento 2 EE edition.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-ee
33
 * @link      http://www.techdivision.com
34
 */
35
class EeProductObserver extends ProductObserver
36
{
37
38
    /**
39
     * Process the observer's business logic.
40
     *
41
     * @return array The processed row
42
     */
43 View Code Duplication
    protected function process()
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...
44
    {
45
46
        // prepare the static entity values
47
        $product = $this->initializeProduct($this->prepareAttributes());
0 ignored issues
show
Bug introduced by
The method initializeProduct() does not seem to exist on object<TechDivision\Impo...vers\EeProductObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
49
        // insert the entity and set the entity ID, SKU and attribute set
50
        $this->setLastRowId($this->persistProduct($product));
51
        $this->setLastEntityId($product[MemberNames::ENTITY_ID]);
52
    }
53
54
    /**
55
     * Prepare the attributes of the entity that has to be persisted.
56
     *
57
     * @return array The prepared attributes
58
     */
59
    protected function prepareAttributes()
60
    {
61
62
        // load the parent attributes
63
        $parentAttr = parent::prepareAttributes();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TechDivision\Import\Prod...servers\ProductObserver as the method prepareAttributes() does only exist in the following sub-classes of TechDivision\Import\Prod...servers\ProductObserver: TechDivision\Import\Prod...rvers\EeProductObserver. 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...
64
65
        // initialize the product values
66
        $attr = array(
67
            MemberNames::ENTITY_ID  => $this->nextIdentifier(),
68
            MemberNames::CREATED_IN => 1,
69
            MemberNames::UPDATED_IN => strtotime('+20 years')
70
        );
71
72
        // merge and return the attributes
73
        return array_merge($parentAttr, $attr);
74
    }
75
76
    /**
77
     * Set's the row ID of the product that has been created recently.
78
     *
79
     * @param string $rowId The row ID
80
     *
81
     * @return void
82
     */
83
    protected function setLastRowId($rowId)
84
    {
85
        $this->getSubject()->setLastRowId($rowId);
86
    }
87
88
    /**
89
     * Return's the next available product entity ID.
90
     *
91
     * @return integer The next available product entity ID
92
     */
93
    protected function nextIdentifier()
94
    {
95
        return $this->getSubject()->nextIdentifier();
96
    }
97
}
98