Completed
Push — master ( ee291f...d6b81e )
by Tim
11s
created

persistProductLinkAttributeInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Link\Observers\LinkAttributePositionObserver
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-link
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Link\Observers;
22
23
use TechDivision\Import\Product\Link\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Link\Utils\MemberNames;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
use TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface;
27
28
/**
29
 * Oberserver that provides functionality for the product link attribute position replace operation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-product-link
35
 * @link      http://www.techdivision.com
36
 */
37
class LinkAttributePositionObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The link attribute that has to be handled by this observer.
42
     *
43
     * @var string
44
     */
45
    const ATTRIBUTE_CODE = 'position';
46
47
    /**
48
     * The product link attribute ID to persist the the position attribute for.
49
     *
50
     * @var integer
51
     */
52
    protected $productLinktAttributeId;
53
54
    /**
55
     * The product link processor instance.
56
     *
57
     * @var \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface
58
     */
59
    protected $productLinkProcessor;
60
61
    /**
62
     * Initialize the observer with the passed product link processor instance.
63
     *
64
     * @param \TechDivision\Import\Product\Link\Services\ProductLinkProcessorInterface $productLinkProcessor The product link processor instance
65
     */
66
    public function __construct(ProductLinkProcessorInterface $productLinkProcessor)
67
    {
68
        $this->productLinkProcessor= $productLinkProcessor;
69
    }
70
71
    /**
72
     * Return's the product link processor instance.
73
     *
74
     * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product link processor instance
75
     */
76
    protected function getProductLinkProcessor()
77
    {
78
        return $this->productLinkProcessor;
79
    }
80
81
    /**
82
     * Process the observer's business logic.
83
     *
84
     * @return array The processed row
85
     */
86
    protected function process()
87
    {
88
89
        // initialize the attribute code
90
        $attributeCode = LinkAttributePositionObserver::ATTRIBUTE_CODE;
91
92
        try {
93
            // extract the link type code from the row
94
            $linkTypeId = $this->mapLinkTypeCodeToLinkTypeId($this->getValue(ColumnKeys::LINK_TYPE_CODE));
95
        } catch (\Exception $e) {
96
            // query whether or not, debug mode is enabled
97
            if ($this->isDebugMode()) {
98
                // log a warning and return immediately
99
                $this->getSystemLogger()->warning($e->getMessage());
100
                return;
101
            }
102
103
            // if we're NOT in debug mode, re-throw the exception
104
            throw $e;
105
        }
106
107
        // try to load the product link attribute
108
        if ($productLinkAttribute = $this->getProductLinkAttribute($linkTypeId, $attributeCode)) {
109
            $this->setProductLinkAttributeId($productLinkAttribute[MemberNames::PRODUCT_LINK_ATTRIBUTE_ID]);
110
        } else {
111
            return;
112
        }
113
114
        // prepare, initialize and persist the product link attribute int entity
115
        $productLink = $this->initializeProductLinkAttributeInt($this->prepareAttributes());
116
        $this->persistProductLinkAttributeInt($productLink);
117
    }
118
119
    /**
120
     * Prepare the attributes of the entity that has to be persisted.
121
     *
122
     * @return array The prepared attributes
123
     */
124
    protected function prepareAttributes()
125
    {
126
127
        // load the ID of the last link
128
        $linkId = $this->getLastLinkId();
129
130
        // load the product link attribute ID
131
        $productLinkAttributeId = $this->getProductLinkAttributeId();
132
133
        // load the position value
134
        $value = $this->getValue(ColumnKeys::LINK_POSITION);
135
136
        // initialize and return the entity
137
        return $this->initializeEntity(
138
            array(
139
                MemberNames::PRODUCT_LINK_ATTRIBUTE_ID => $productLinkAttributeId,
140
                MemberNames::LINK_ID                   => $linkId,
141
                MemberNames::VALUE                     => $value
142
            )
143
        );
144
    }
145
146
    /**
147
     * Temporary persist the product link attribute ID.
148
     *
149
     * @param integer $productLinkAttributeId The product link attribute ID
150
     *
151
     * @return void
152
     */
153
    protected function setProductLinkAttributeId($productLinkAttributeId)
154
    {
155
        $this->productLinktAttributeId = $productLinkAttributeId;
156
    }
157
158
    /**
159
     * Return's the temporary persisted product link attribute ID.
160
     *
161
     * @return integer The product link attribute ID
162
     */
163
    protected function getProductLinkAttributeId()
164
    {
165
        return $this->productLinktAttributeId;
166
    }
167
168
    /**
169
     * Return's the link attribute for the passed link type ID and attribute code.
170
     *
171
     * @param integer $linkTypeId    The link type
172
     * @param string  $attributeCode The attribute code
173
     *
174
     * @return array The link attribute
175
     */
176
    protected function getProductLinkAttribute($linkTypeId, $attributeCode)
177
    {
178
        return $this->getSubject()->getProductLinkAttribute($linkTypeId, $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 getProductLinkAttribute() does only exist in the following implementations of said interface: TechDivision\Import\Prod...nk\Subjects\LinkSubject.

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...
179
    }
180
181
    /**
182
     * Initialize the product link attribute with the passed attributes and returns an instance.
183
     *
184
     * @param array $attr The product link attribute
185
     *
186
     * @return array The initialized product link attribute
187
     */
188
    protected function initializeProductLinkAttributeInt(array $attr)
189
    {
190
        return $attr;
191
    }
192
193
    /**
194
     * Load the temporary persisted the last link ID.
195
     *
196
     * @return integer The last link ID
197
     */
198
    protected function getLastLinkId()
199
    {
200
        return $this->getSubject()->getLastLinkId();
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 getLastLinkId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...nk\Subjects\LinkSubject.

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...
201
    }
202
203
    /**
204
     * Return the link type ID for the passed link type code.
205
     *
206
     * @param string $linkTypeCode The link type code to return the link type ID for
207
     *
208
     * @return integer The mapped link type ID
209
     * @throws \Exception Is thrown if the link type code is not mapped yet
210
     */
211
    protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode)
212
    {
213
        return $this->getSubject()->mapLinkTypeCodeToLinkTypeId($linkTypeCode);
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 mapLinkTypeCodeToLinkTypeId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...nk\Subjects\LinkSubject.

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...
214
    }
215
216
    /**
217
     * Persist's the passed product link attribute int data and return's the ID.
218
     *
219
     * @param array $productLinkAttributeInt The product link attribute int data to persist
220
     *
221
     * @return string The ID of the persisted entity
222
     */
223
    protected function persistProductLinkAttributeInt($productLinkAttributeInt)
224
    {
225
        $this->getProductLinkProcessor()->persistProductLinkAttributeInt($productLinkAttributeInt);
226
    }
227
}
228