Completed
Push — 10.x ( 0c1414 )
by Tim
08:49
created

getProductLinkAttribute()   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 2
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\Link\Services\ProductLinkProcessorInterface 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
        // process the link type attributes
90
        $productLinkAttribute = $this->getProductLinkAttributeByLinkTypeCodeAndAttributeCode($this->getValue(ColumnKeys::LINK_TYPE_CODE), $this->getValue(ColumnKeys::LINK_TYPE_ATTRIBUTE_CODE));
91
92
        // try to load the product link attribute
93
        $this->setProductLinkAttributeId($productLinkAttribute[MemberNames::PRODUCT_LINK_ATTRIBUTE_ID]);
94
95
        // load the link attribute data type
96
        $dataType = $productLinkAttribute[MemberNames::DATA_TYPE];
97
98
        // concatenate the initialize/persist method names
99
        $intializeMethod = sprintf('initializeProductLinkAttribute%s', $dataType);
100
        $persistMethod = sprintf('persistProductLinkAttribute%s', $dataType);
101
102
        // prepare, initialize and persist the product link attribute int entity
103
        $this->$persistMethod($this->$intializeMethod($this->prepareAttributes()));
104
    }
105
106
    /**
107
     * Prepare the attributes of the entity that has to be persisted.
108
     *
109
     * @return array The prepared attributes
110
     */
111
    protected function prepareAttributes()
112
    {
113
114
        // load the ID of the last link
115
        $linkId = $this->getLastLinkId();
116
117
        // load the product link attribute ID
118
        $productLinkAttributeId = $this->getProductLinkAttributeId();
119
120
        // load the position value
121
        $value = $this->getValue(ColumnKeys::LINK_TYPE_ATTRIBUTE_VALUE);
122
123
        // initialize and return the entity
124
        return $this->initializeEntity(
125
            array(
126
                MemberNames::PRODUCT_LINK_ATTRIBUTE_ID => $productLinkAttributeId,
127
                MemberNames::LINK_ID                   => $linkId,
128
                MemberNames::VALUE                     => $value
129
            )
130
        );
131
    }
132
133
    /**
134
     * Temporary persist the product link attribute ID.
135
     *
136
     * @param integer $productLinkAttributeId The product link attribute ID
137
     *
138
     * @return void
139
     */
140
    protected function setProductLinkAttributeId($productLinkAttributeId)
141
    {
142
        $this->productLinktAttributeId = $productLinkAttributeId;
143
    }
144
145
    /**
146
     * Return's the temporary persisted product link attribute ID.
147
     *
148
     * @return integer The product link attribute ID
149
     */
150
    protected function getProductLinkAttributeId()
151
    {
152
        return $this->productLinktAttributeId;
153
    }
154
155
    /**
156
     * Return's the link attribute for the passed link type and attribute code.
157
     *
158
     * @param string $linkTypeCode  The link type code
159
     * @param string $attributeCode The attribute code
160
     *
161
     * @return array The link attribute
162
     */
163
    protected function getProductLinkAttributeByLinkTypeCodeAndAttributeCode($linkTypeCode, $attributeCode)
164
    {
165
        return $this->getSubject()->getProductLinkAttributeByLinkTypeCodeAndAttributeCode($linkTypeCode, $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 getProductLinkAttributeB...eCodeAndAttributeCode() does only exist in the following implementations of said interface: TechDivision\Import\Prod...nk\Subjects\LinkSubject, TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\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...
166
    }
167
168
    /**
169
     * Initialize the product link attribute with the passed attributes and returns an instance.
170
     *
171
     * @param array $attr The product link attribute
172
     *
173
     * @return array The initialized product link attribute
174
     */
175
    protected function initializeProductLinkAttributeInt(array $attr)
176
    {
177
        return $attr;
178
    }
179
180
    /**
181
     * Initialize the product link attribute with the passed attributes and returns an instance.
182
     *
183
     * @param array $attr The product link attribute
184
     *
185
     * @return array The initialized product link attribute
186
     */
187
    protected function initializeProductLinkAttributeDecimal(array $attr)
188
    {
189
        return $attr;
190
    }
191
192
    /**
193
     * Initialize the product link attribute with the passed attributes and returns an instance.
194
     *
195
     * @param array $attr The product link attribute
196
     *
197
     * @return array The initialized product link attribute
198
     */
199
    protected function initializeProductLinkAttributeVarchar(array $attr)
200
    {
201
        return $attr;
202
    }
203
204
    /**
205
     * Load the temporary persisted the last link ID.
206
     *
207
     * @return integer The last link ID
208
     */
209
    protected function getLastLinkId()
210
    {
211
        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...
212
    }
213
214
    /**
215
     * Return the link type ID for the passed link type code.
216
     *
217
     * @param string $linkTypeCode The link type code to return the link type ID for
218
     *
219
     * @return integer The mapped link type ID
220
     * @throws \Exception Is thrown if the link type code is not mapped yet
221
     */
222
    protected function mapLinkTypeCodeToLinkTypeId($linkTypeCode)
223
    {
224
        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, TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\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...
225
    }
226
227
    /**
228
     * Persists the passed product link attribute int data and return's the ID.
229
     *
230
     * @param array $productLinkAttributeInt The product link attribute int data to persist
231
     *
232
     * @return void
233
     */
234
    protected function persistProductLinkAttributeInt($productLinkAttributeInt)
235
    {
236
        $this->getProductLinkProcessor()->persistProductLinkAttributeInt($productLinkAttributeInt);
237
    }
238
239
    /**
240
     * Persists the passed product link attribute decimal data and return's the ID.
241
     *
242
     * @param array $productLinkAttributeDecimal The product link attribute decimal data to persist
243
     *
244
     * @return void
245
     */
246
    protected function persistProductLinkAttributeDecimal($productLinkAttributeDecimal)
247
    {
248
        $this->getProductLinkProcessor()->persistProductLinkAttributeDecimal($productLinkAttributeDecimal);
249
    }
250
251
    /**
252
     * Persists the passed product link attribute varchar data and return's the ID.
253
     *
254
     * @param array $productLinkAttributeVarchar The product link attribute varchar data to persist
255
     *
256
     * @return void
257
     */
258
    protected function persistProductLinkAttributeVarchar($productLinkAttributeVarchar)
259
    {
260
        $this->getProductLinkProcessor()->persistProductLinkAttributeVarchar($productLinkAttributeVarchar);
261
    }
262
}
263