Completed
Pull Request — master (#15)
by Tim
01:50
created

PreLoadAttributeOptionIdObserver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 26.51 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 22
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAttributeBunchProcessor() 0 4 1
A process() 22 22 2
A hasBeenProcessed() 0 4 1
A preLoadOptionId() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\PreLoadAttributeOptionIdObserver
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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Attribute\Utils\ColumnKeys;
25
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
26
27
/**
28
 * Observer that pre-loads the option ID of the EAV attribute option with the attribute code/value found in the CSV file.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-attribute
34
 * @link      http://www.techdivision.com
35
 */
36
class PreLoadAttributeOptionIdObserver extends AbstractAttributeImportObserver
37
{
38
39
    /**
40
     * The attribute processor instance.
41
     *
42
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
43
     */
44
    protected $attributeBunchProcessor;
45
46
    /**
47
     * Initializes the observer with the passed subject instance.
48
     *
49
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
50
     */
51
    public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor)
52
    {
53
        $this->attributeBunchProcessor = $attributeBunchProcessor;
54
    }
55
56
    /**
57
     * Return's the attribute bunch processor instance.
58
     *
59
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
60
     */
61
    protected function getAttributeBunchProcessor()
62
    {
63
        return $this->attributeBunchProcessor;
64
    }
65
66
    /**
67
     * Process the observer's business logic.
68
     *
69
     * @return array The processed row
70
     */
71 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...
72
    {
73
74
        // load the store value and the attribute code
75
        $value = $this->getValue(ColumnKeys::ADMIN_STORE_VALUE);
76
        $attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE);
77
78
        // query whether or not, we've found a new attribute code => means we've found a new EAV attribute
79
        if ($this->hasBeenProcessed($attributeCode, $value)) {
80
            return;
81
        }
82
83
        // load the ID of the admin store
84
        $storeId = $this->getStoreId(StoreViewCodes::ADMIN);
85
86
        // load the EAV attribute option with the passed value
87
        $attributeOption = $this->getAttributeBunchProcessor()
88
                                ->loadAttributeOptionByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value);
89
90
        // preserve the attribute ID for the passed EAV attribute option
91
        $this->preLoadOptionId($attributeOption);
92
    }
93
94
    /**
95
     * Queries whether or not the option with the passed code/value has already been processed.
96
     *
97
     * @param string $attributeCode The attribute code to check
98
     * @param string $value         The option value to check
99
     *
100
     * @return boolean TRUE if the path has been processed, else FALSE
101
     */
102
    protected function hasBeenProcessed($attributeCode, $value)
103
    {
104
        return $this->getSubject()->hasBeenProcessed($attributeCode, $value);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method hasBeenProcessed() does only exist in the following implementations of said interface: TechDivision\Import\Attr...e\Subjects\BunchSubject, TechDivision\Import\Attr...\Subjects\OptionSubject.

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...
105
    }
106
107
    /**
108
     * Pre-load the option ID for the passed EAV attribute option.
109
     *
110
     * @param array $attributeOption The EAV attribute option with the ID that has to be pre-loaded
111
     *
112
     * @return void
113
     */
114
    protected function preLoadOptionId(array $attributeOption)
115
    {
116
        return $this->getSubject()->preLoadOptionId($attributeOption);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method preLoadOptionId() does only exist in the following implementations of said interface: TechDivision\Import\Attr...\Subjects\OptionSubject.

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...
117
    }
118
}
119