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

PreLoadAttributeIdObserver::hasBeenProcessed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\PreLoadAttributeIdObserver
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\Attribute\Utils\ColumnKeys;
24
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
25
26
/**
27
 * Observer that pre-loads the attribute ID of the EAV attribute with the code found in the CSV file.
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-attribute
33
 * @link      http://www.techdivision.com
34
 */
35
class PreLoadAttributeIdObserver extends AbstractAttributeImportObserver
36
{
37
38
    /**
39
     * The attribute processor instance.
40
     *
41
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
42
     */
43
    protected $attributeBunchProcessor;
44
45
    /**
46
     * Initializes the observer with the passed subject instance.
47
     *
48
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
49
     */
50
    public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor)
51
    {
52
        $this->attributeBunchProcessor = $attributeBunchProcessor;
53
    }
54
55
    /**
56
     * Return's the attribute bunch processor instance.
57
     *
58
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
59
     */
60
    protected function getAttributeBunchProcessor()
61
    {
62
        return $this->attributeBunchProcessor;
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return array The processed row
69
     */
70
    protected function process()
71
    {
72
73
        // query whether or not, we've found a new attribute code => means we've found a new EAV attribute
74
        if ($this->hasBeenProcessed($attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE))) {
75
            return;
76
        }
77
78
        // preserve the attribute ID for the EAV attribute with the passed code
79
        if ($attribute = $this->loadAttributeByAttributeCode($attributeCode)) {
80
            $this->preLoadAttributeId($attribute);
81
        }
82
    }
83
84
    /**
85
     * Queries whether or not the attribute with the passed code has already been processed.
86
     *
87
     * @param string $attributeCode The attribute code to check
88
     *
89
     * @return boolean TRUE if the path has been processed, else FALSE
90
     */
91
    protected function hasBeenProcessed($attributeCode)
92
    {
93
        return $this->getSubject()->hasBeenProcessed($attributeCode);
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...
94
    }
95
96
    /**
97
     * Pre-load and temporary persist the attribute ID for the passed EAV attribute.
98
     *
99
     * @param array $attribute The EAV attribute to pre-load
100
     *
101
     * @return void
102
     */
103
    protected function preLoadAttributeId(array $attribute)
104
    {
105
        return $this->getSubject()->preLoadAttributeId($attribute);
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 preLoadAttributeId() does only exist in the following implementations of said interface: TechDivision\Import\Attr...e\Subjects\BunchSubject.

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...
106
    }
107
108
    /**
109
     * Load's and return's the EAV attribute with the passed code.
110
     *
111
     * @param string $attributeCode The code of the EAV attribute to load
112
     *
113
     * @return array The EAV attribute
114
     */
115
    protected function loadAttributeByAttributeCode($attributeCode)
116
    {
117
        return $this->getAttributeBunchProcessor()->loadAttributeByAttributeCode($attributeCode);
118
    }
119
}
120