Completed
Push — 24.x ( d7fb53...29905b )
by Tim
01:37
created

getProductUrlRewriteProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserver
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-url-rewrite
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\UrlRewrite\Observers;
22
23
use TechDivision\Import\Product\UrlRewrite\Utils\CoreConfigDataKeys;
24
use TechDivision\Import\Utils\StoreViewCodes;
25
use TechDivision\Import\Product\Utils\VisibilityKeys;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
use TechDivision\Import\Product\UrlRewrite\Utils\ColumnKeys;
28
use TechDivision\Import\Product\UrlRewrite\Utils\MemberNames;
29
use TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface;
30
use TechDivision\Import\Subjects\SubjectInterface;
31
32
/**
33
 * Observer that creates/updates the product's URL rewrites.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2016 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-product-url-rewrite
39
 * @link      http://www.techdivision.com
40
 */
41
class UrlRewriteObserver extends AbstractProductImportObserver
42
{
43
44
    /**
45
     * The entity type to load the URL rewrites for.
46
     *
47
     * @var string
48
     */
49
    const ENTITY_TYPE = 'product';
50
51
    /**
52
     * The key for the category in the metadata.
53
     *
54
     * @var string
55
     */
56
    const CATEGORY_ID = 'category_id';
57
58
    /**
59
     * The URL key from the CSV file column that has to be processed by the observer.
60
     *
61
     * @var string
62
     */
63
    protected $urlKey;
64
65
    /**
66
     * The actual category ID to process.
67
     *
68
     * @var integer
69
     */
70
    protected $categoryId;
71
72
    /**
73
     * The actual entity ID to process.
74
     *
75
     * @var integer
76
     */
77
    protected $entityId;
78
79
    /**
80
     * The ID of the recently created URL rewrite.
81
     *
82
     * @var integer
83
     */
84
    protected $urlRewriteId;
85
86
    /**
87
     * The array with the URL rewrites that has to be created.
88
     *
89
     * @var array
90
     */
91
    protected $urlRewrites = array();
92
93
    /**
94
     * The array with the category IDs related with the product.
95
     *
96
     * @var array
97
     */
98
    protected $productCategoryIds = array();
99
100
    /**
101
     * The product bunch processor instance.
102
     *
103
     * @var \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface
104
     */
105
    protected $productUrlRewriteProcessor;
106
107
    /**
108
     * Initialize the observer with the passed product URL rewrite processor instance.
109
     *
110
     * @param \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface $productUrlRewriteProcessor The product URL rewrite processor instance
111
     */
112 5
    public function __construct(ProductUrlRewriteProcessorInterface $productUrlRewriteProcessor)
113
    {
114 5
        $this->productUrlRewriteProcessor = $productUrlRewriteProcessor;
115 5
    }
116
117
    /**
118
     * Return's the product bunch processor instance.
119
     *
120
     * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance
121
     */
122 3
    protected function getProductUrlRewriteProcessor()
123
    {
124 3
        return $this->productUrlRewriteProcessor;
125
    }
126
127
    /**
128
     * Will be invoked by the action on the events the listener has been registered for.
129
     *
130
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
131
     *
132
     * @return array The modified row
133
     * @throws \Exception Is thrown, if the product is not available or no URL key has been specified
134
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
135
     */
136 3
    public function handle(SubjectInterface $subject)
137
    {
138
139
        // initialize the row
140 3
        $this->setSubject($subject);
141 3
        $this->setRow($subject->getRow());
142
143
        // try to load the entity ID for the product with the passed SKU
144 3
        if ($product = $this->loadProduct($sku = $this->getValue(ColumnKeys::SKU))) {
145 3
            $this->setLastEntityId($this->entityId = $product[MemberNames::ENTITY_ID]);
146 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
147
            // prepare a log message
148
            $message = sprintf('Product with SKU "%s" can\'t be loaded to create URL rewrites', $sku);
149
            // query whether or not we're in debug mode
150
            if ($this->getSubject()->isDebugMode()) {
151
                $this->getSubject()->getSystemLogger()->warning($message);
152
                return $this->getRow();
153
            } else {
154
                throw new \Exception($this->appendExceptionSuffix($message));
155
            }
156
        }
157
158
        // try to load the URL key
159 3
        if ($this->hasValue(ColumnKeys::URL_KEY)) {
160 3
            $this->urlKey = $this->getValue(ColumnKeys::URL_KEY);
161 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
162
            // prepare a log message
163
            $message = sprintf('Can\'t find a value in column "url_key" for product with SKU "%s"', $sku);
164
            // query whether or not we're in debug mode
165
            if ($this->getSubject()->isDebugMode()) {
166
                $this->getSubject()->getSystemLogger()->warning($message);
167
                return $this->getRow();
168
            } else {
169
                throw new \Exception($this->appendExceptionSuffix($message));
170
            }
171
        }
172
173
        // initialize the store view code
174 3
        $this->getSubject()->prepareStoreViewCode();
175
176
        // load the store view - if no store view has been set we use the admin store view as default
177 3
        $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN);
178
179
        // query whether or not the row has already been processed
180 3 View Code Duplication
        if ($this->storeViewHasBeenProcessed($sku, $storeViewCode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
181
            // log a message
182
            $this->getSubject()
183
                ->getSystemLogger()
184
                ->debug(
185
                    sprintf(
186
                        'URL rewrites for SKU "%s" + store view code "%s" has already been processed',
187
                        $sku,
188
                        $storeViewCode
189
                    )
190
                );
191
192
            // return without creating any rewrites
193
            return $this->getRow();
194
        };
195
196
        // stop processing as we don't want to create URL rewrites for the admin store view
197 3 View Code Duplication
        if ($storeViewCode === StoreViewCodes::ADMIN) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
198
            // log a message and return
199
            $this->getSubject()
200
                 ->getSystemLogger()
201
                 ->debug(
202
                     sprintf(
203
                         'Store with code "%s" is not active, no URL rewrites will be created for product with SKU "%s"',
204
                         $storeViewCode,
205
                         $sku
206
                     )
207
                 );
208
209
            // return without creating any rewrites
210
            return $this->getRow();
211
        }
212
213
        // stop processing if the store is NOT active
214 3 View Code Duplication
        if (!$this->getSubject()->storeIsActive($storeViewCode)) {
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 storeIsActive() does only exist in the following implementations of said interface: TechDivision\Import\Prod...jects\UrlRewriteSubject.

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...
Duplication introduced by
This code seems to be duplicated across 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...
215
            // log a message and return
216
            $this->getSubject()
217
                 ->getSystemLogger()
218
                 ->debug(
219
                     sprintf(
220
                         'Store with code "%s" is not active, no URL rewrites will be created for product with SKU "%s"',
221
                         $storeViewCode,
222
                         $sku
223
                     )
224
                 );
225
226
            // return without creating any rewrites
227
            return $this->getRow();
228
        }
229
230
        // only map the visibility for the product row related to the default store view
231 3
        if (!$this->hasBeenProcessed($sku)) {
232 3
            $this->addEntityIdVisibilityIdMapping($this->getValue(ColumnKeys::VISIBILITY));
233
        }
234
235
        // process the functionality and return the row
236 3
        $this->process();
237
238
        // return the processed row
239 3
        return $this->getRow();
240
    }
241
242
    /**
243
     * Process the observer's business logic.
244
     *
245
     * @return void
246
     */
247 3
    protected function process()
248
    {
249
250
        // prepare the URL rewrites
251 3
        $this->prepareUrlRewrites();
252
253
        // iterate over the categories and create the URL rewrites
254 3
        foreach ($this->urlRewrites as $categoryId => $urlRewrite) {
255
            // initialize and persist the URL rewrite
256 3
            if ($urlRewrite = $this->initializeUrlRewrite($urlRewrite)) {
257
                // initialize URL rewrite and catagory ID
258 3
                $this->categoryId = $categoryId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $categoryId can also be of type string. However, the property $categoryId is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
259
260
                try {
261
                    // persist the URL rewrite
262 3
                    $this->urlRewriteId = $this->persistUrlRewrite($urlRewrite);
0 ignored issues
show
Documentation Bug introduced by
The property $urlRewriteId was declared of type integer, but $this->persistUrlRewrite($urlRewrite) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
263
264
                    /*
265
                     * Attention! Stop processing, if this is a root category, because Magento needs explicitly
266
                     * NO URL rewrite product category relation to render canonical and meta og:url tag!
267
                     */
268 3
                    if ($this->isRootCategory($this->getCategory($categoryId))) {
269 3
                        continue;
270
                    }
271
272
                    // initialize and persist the URL rewrite product => category relation
273 2
                    $urlRewriteProductCategory = $this->initializeUrlRewriteProductCategory(
274 2
                        $this->prepareUrlRewriteProductCategoryAttributes()
275
                    );
276
277
                    // persist the URL rewrite product category relation
278 2
                    $this->persistUrlRewriteProductCategory($urlRewriteProductCategory);
279
                } catch (\Exception $e) {
280
                    // query whether or not debug mode has been enabled
281 View Code Duplication
                    if ($this->getSubject()->isDebugMode()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
282
                        $this->getSubject()
283
                             ->getSystemLogger()
284
                             ->warning($this->getSubject()->appendExceptionSuffix($e->getMessage()));
285
                    } else {
286
                        throw $e;
287
                    }
288
                }
289
            }
290
        }
291 3
    }
292
293
    /**
294
     * Initialize the category product with the passed attributes and returns an instance.
295
     *
296
     * @param array $attr The category product attributes
297
     *
298
     * @return array The initialized category product
299
     */
300 2
    protected function initializeUrlRewrite(array $attr)
301
    {
302 2
        return $attr;
303
    }
304
305
    /**
306
     * Initialize the URL rewrite product => category relation with the passed attributes
307
     * and returns an instance.
308
     *
309
     * @param array $attr The URL rewrite product => category relation attributes
310
     *
311
     * @return array The initialized URL rewrite product => category relation
312
     */
313 1
    protected function initializeUrlRewriteProductCategory($attr)
314
    {
315 1
        return $attr;
316
    }
317
318
    /**
319
     * Prepare's the URL rewrites that has to be created/updated.
320
     *
321
     * @return void
322
     */
323 3
    protected function prepareUrlRewrites()
324
    {
325
326
        // (re-)initialize the array for the URL rewrites and the product category IDs
327 3
        $this->urlRewrites = array();
328 3
        $this->productCategoryIds = array();
329
330
        // do NOT create new URL rewrites, if the product is NOT visible (any more), BUT
331
        // handle existing URL rewrites, e. g. to remove and clean up the URL rewrites
332 3
        if (!$this->isVisible()) {
333
             // log a message
334
             $this->getSubject()
335
                 ->getSystemLogger()
336
                 ->debug(
337
                     sprintf(
338
                         'Product with SKU "%s" is not visible, so no URL rewrites will be created',
339
                         $this->getValue(ColumnKeys::SKU)
340
                     )
341
                 );
342
343
             // return without creating any rewrites
344
             return;
345
        }
346
347
        // load the root category, because we need that to create the default product URL rewrite
348 3
        $rootCategory = $this->getRootCategory();
349
350
        // at least, add the root category ID to the category => product relations
351 3
        $this->productCategoryIds[] = $rootCategory[MemberNames::ENTITY_ID];
352
353
        // load the store view code from the appropriate column
354 3
        $storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE);
355
356
        // append the category => product relations found
357 3
        foreach ($this->getValue(ColumnKeys::CATEGORIES, array(), array($this, 'explode')) as $path) {
358
            try {
359
                // downgrade the path
360 2
                $path = implode('/', $this->explode($path, '/'));
361
                // try to load the category for the given path
362 2
                $category = $this->getCategoryByPath(trim($path), $storeViewCode);
363
                // resolve the product's categories recursively
364 2
                $this->resolveCategoryIds($category[MemberNames::ENTITY_ID], true, $storeViewCode);
365
            } catch (\Exception $e) {
366
                // query whether or not debug mode has been enabled
367 View Code Duplication
                if ($this->getSubject()->isDebugMode()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
368
                    $this->getSubject()
369
                         ->getSystemLogger()
370
                         ->warning($this->getSubject()->appendExceptionSuffix($e->getMessage()));
371
                } else {
372
                    throw $e;
373
                }
374
            }
375
        }
376
377
        // prepare the URL rewrites
378 3
        foreach ($this->productCategoryIds as $categoryId) {
379
            // set the category ID
380 3
            $this->categoryId = $categoryId;
381
382
            // prepare the attributes for each URL rewrite
383 3
            $this->urlRewrites[$categoryId] = $this->prepareAttributes($storeViewCode);
384
        }
385 3
    }
386
387
    /**
388
     * Resolve's the parent categories of the category with the passed ID and relate's
389
     * it with the product with the passed ID, if the category is top level OR has the
390
     * anchor flag set.
391
     *
392
     * @param integer $categoryId    The ID of the category to resolve the parents
393
     * @param boolean $topLevel      TRUE if the passed category has top level, else FALSE
394
     * @param string  $storeViewCode The store view code to resolve the category IDs for
395
     *
396
     * @return void
397
     */
398 2
    protected function resolveCategoryIds($categoryId, $topLevel = false, $storeViewCode = StoreViewCodes::ADMIN)
399
    {
400
401
        // return immediately if this is the absolute root node
402 2
        if ((integer) $categoryId === 1) {
403 2
            return;
404
        }
405
406
        // load the data of the category with the passed ID
407 2
        $category = $this->getCategory($categoryId, $storeViewCode);
408
409
        // create the product category relation for the current category
410 2
        $this->createProductCategoryRelation($category, $topLevel);
411
412
        // load the root category
413 2
        $rootCategory = $this->getRootCategory();
414
415
        // try to resolve the parent category IDs
416 2
        if ($rootCategory[MemberNames::ENTITY_ID] !== ($parentId = $category[MemberNames::PARENT_ID])) {
417 2
            $this->resolveCategoryIds($parentId, false);
418
        }
419 2
    }
420
421
    /**
422
     * Adds the entity product relation if necessary.
423
     *
424
     * @param array   $category The category to create the relation for
425
     * @param boolean $topLevel Whether or not the category has top level
426
     *
427
     * @return void
428
     */
429 4
    private function createProductCategoryRelation($category, $topLevel)
430
    {
431
432
        // query whether or not the product has already been related
433 4
        if (in_array($category[MemberNames::ENTITY_ID], $this->productCategoryIds)) {
434 2
            return;
435
        }
436
437
        // load the backend configuration value for whether or not the catalog product rewrites should be generated
438 4
        $generateCategoryRewrites = $this->getGenerateCategoryProductRewritesOptionValue();
439
440
        // abort if generating product categories is disabled and category is not root
441 4
        if ($generateCategoryRewrites === false && $this->isRootCategory($category) === false) {
442 1
            return;
443
        }
444
445
        // create relation if the category is top level or has the anchor flag set
446 3
        if ($topLevel || (integer) $category[MemberNames::IS_ANCHOR] === 1) {
447 3
            $this->productCategoryIds[] = $category[MemberNames::ENTITY_ID];
448 3
            return;
449
        }
450
451
        // log a debug messsage that the URL rewrite has not
452
        // been created because of the missing anchor flag
453 2
        $this->getSubject()
454 2
            ->getSystemLogger()
455 2
            ->debug(
456 2
                sprintf(
457 2
                    'Don\'t create URL rewrite for category "%s" because of missing anchor flag',
458 2
                    $category[MemberNames::PATH]
459
                )
460
            );
461 2
    }
462
463
    /**
464
     * Returns the option value for whether or not to generate product catalog rewrites as well.
465
     *
466
     * @return bool
467
     */
468 4
    private function getGenerateCategoryProductRewritesOptionValue()
469
    {
470 4
        return (bool) $this->getSubject()->getCoreConfigData(
471 4
            CoreConfigDataKeys::CATALOG_SEO_GENERATE_CATEGORY_PRODUCT_REWRITES,
472 4
            true
473
        );
474
    }
475
476
    /**
477
     * Prepare the attributes of the entity that has to be persisted.
478
     *
479
     * @param string $storeViewCode The store view code to prepare the attributes for
480
     *
481
     * @return array The prepared attributes
482
     */
483 3
    protected function prepareAttributes($storeViewCode)
484
    {
485
486
        // load the store ID to use
487 3
        $storeId = $this->getSubject()->getRowStoreId();
0 ignored issues
show
Bug introduced by
The method getRowStoreId() does not exist on TechDivision\Import\Subjects\SubjectInterface. Did you maybe mean getRow()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
488
489
        // load the category to create the URL rewrite for
490 3
        $category = $this->getCategory($this->categoryId, $storeViewCode);
491
492
        // initialize the values
493 3
        $metadata = $this->prepareMetadata($category);
494 3
        $targetPath = $this->prepareTargetPath($category);
495 3
        $requestPath = $this->prepareRequestPath($category);
496
497
        // return the prepared URL rewrite
498 3
        return $this->initializeEntity(
499
            array(
500 3
                MemberNames::ENTITY_TYPE      => UrlRewriteObserver::ENTITY_TYPE,
501 3
                MemberNames::ENTITY_ID        => $this->entityId,
502 3
                MemberNames::REQUEST_PATH     => $requestPath,
503 3
                MemberNames::TARGET_PATH      => $targetPath,
504 3
                MemberNames::REDIRECT_TYPE    => 0,
505 3
                MemberNames::STORE_ID         => $storeId,
506 3
                MemberNames::DESCRIPTION      => null,
507 3
                MemberNames::IS_AUTOGENERATED => 1,
508 3
                MemberNames::METADATA         => $metadata ? json_encode($metadata) : null
509
            )
510
        );
511
    }
512
513
    /**
514
     * Prepare's the URL rewrite product => category relation attributes.
515
     *
516
     * @return array The prepared attributes
517
     */
518 2
    protected function prepareUrlRewriteProductCategoryAttributes()
519
    {
520
521
        // return the prepared product
522 2
        return $this->initializeEntity(
523
            array(
524 2
                MemberNames::PRODUCT_ID     => $this->entityId,
525 2
                MemberNames::CATEGORY_ID    => $this->categoryId,
526 2
                MemberNames::URL_REWRITE_ID => $this->urlRewriteId
527
            )
528
        );
529
    }
530
531
    /**
532
     * Prepare's the target path for a URL rewrite.
533
     *
534
     * @param array $category The categroy with the URL path
535
     *
536
     * @return string The target path
537
     */
538 3
    protected function prepareTargetPath(array $category)
539
    {
540
541
        // query whether or not, the category is the root category
542 3
        if ($this->isRootCategory($category)) {
543 3
            $targetPath = sprintf('catalog/product/view/id/%d', $this->entityId);
544
        } else {
545 2
            $targetPath = sprintf('catalog/product/view/id/%d/category/%d', $this->entityId, $category[MemberNames::ENTITY_ID]);
546
        }
547
548
        // return the target path
549 3
        return $targetPath;
550
    }
551
552
    /**
553
     * Prepare's the request path for a URL rewrite or the target path for a 301 redirect.
554
     *
555
     * @param array $category The categroy with the URL path
556
     *
557
     * @return string The request path
558
     * @throws \RuntimeException Is thrown, if the passed category has no or an empty value for attribute "url_path"
559
     */
560 3
    protected function prepareRequestPath(array $category)
561
    {
562
563
        // load the product URL suffix to use
564 3
        $urlSuffix = $this->getSubject()->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_PRODUCT_URL_SUFFIX, '.html');
565
566
        // query whether or not, the category is the root category
567 3
        if ($this->isRootCategory($category)) {
568 3
            return sprintf('%s%s', $this->urlKey, $urlSuffix);
569
        } else {
570
            // query whether or not the category's "url_path" attribute, necessary to create a valid "request_path", is available
571 2
            if (isset($category[MemberNames::URL_PATH]) && $category[MemberNames::URL_PATH]) {
572 2
                return sprintf('%s/%s%s', $category[MemberNames::URL_PATH], $this->urlKey, $urlSuffix);
573
            }
574
        }
575
576
        // throw an exception if the category's "url_path" attribute is NOT available
577
        throw new \RuntimeException(
578
            $this->appendExceptionSuffix(
579
                sprintf(
580
                    'Can\'t find mandatory attribute "%s" for category ID "%d", necessary to build a valid "request_path"',
581
                    MemberNames::URL_PATH,
582
                    $category[MemberNames::ENTITY_ID]
583
                )
584
            )
585
        );
586
    }
587
588
    /**
589
     * Prepare's the URL rewrite's metadata with the passed category values.
590
     *
591
     * @param array $category The category used for preparation
592
     *
593
     * @return array|null The metadata
594
     */
595 3
    protected function prepareMetadata(array $category)
596
    {
597
598
        // initialize the metadata
599 3
        $metadata = array();
600
601
        // query whether or not, the passed category IS the root category
602 3
        if ($this->isRootCategory($category)) {
603 3
            return;
604
        }
605
606
        // if not, set the category ID in the metadata
607 2
        $metadata[UrlRewriteObserver::CATEGORY_ID] = (string) $category[MemberNames::ENTITY_ID];
608
609
        // return the metadata
610 2
        return $metadata;
611
    }
612
613
    /**
614
     * Query whether or not the actual entity is visible.
615
     *
616
     * @return boolean TRUE if the entity is visible, else FALSE
617
     */
618 3
    protected function isVisible()
619
    {
620 3
        return $this->getEntityIdVisibilityIdMapping() !== VisibilityKeys::VISIBILITY_NOT_VISIBLE;
621
    }
622
623
    /**
624
     * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created
625
     * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will
626
     * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>.
627
     *
628
     * @return integer The visibility ID
629
     * @throws \Exception Is thrown, if the entity ID has not been mapped
630
     * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue()
631
     */
632 3
    protected function getEntityIdVisibilityIdMapping()
633
    {
634 3
        return $this->getSubject()->getEntityIdVisibilityIdMapping();
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 getEntityIdVisibilityIdMapping() does only exist in the following implementations of said interface: TechDivision\Import\Prod...jects\UrlRewriteSubject.

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...
635
    }
636
637
    /**
638
     * Return's the root category for the actual view store.
639
     *
640
     * @return array The store's root category
641
     * @throws \Exception Is thrown if the root category for the passed store code is NOT available
642
     */
643 3
    protected function getRootCategory()
644
    {
645 3
        return $this->getSubject()->getRootCategory();
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 getRootCategory() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Prod...jects\UrlRewriteSubject, TechDivision\Import\Subjects\AbstractEavSubject, TechDivision\Import\Subjects\AbstractSubject, TechDivision\Import\Subjects\MoveFilesSubject, TechDivision\Import\Subjects\ValidatorSubject.

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...
646
    }
647
648
    /**
649
     * Return's TRUE if the passed category IS the root category, else FALSE.
650
     *
651
     * @param array $category The category to query
652
     *
653
     * @return boolean TRUE if the passed category IS the root category
654
     */
655 3
    protected function isRootCategory(array $category)
656
    {
657
658
        // load the root category
659 3
        $rootCategory = $this->getRootCategory();
660
661
        // compare the entity IDs and return the result
662 3
        return $rootCategory[MemberNames::ENTITY_ID] === $category[MemberNames::ENTITY_ID];
663
    }
664
665
    /**
666
     * Return's the category with the passed path.
667
     *
668
     * @param string $path          The path of the category to return
669
     * @param string $storeViewCode The store view code of the category to return
670
     *
671
     * @return array The category
672
     */
673 2
    protected function getCategoryByPath($path, $storeViewCode = StoreViewCodes::ADMIN)
674
    {
675 2
        return $this->getSubject()->getCategoryByPath($path, $storeViewCode);
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 getCategoryByPath() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Prod...jects\UrlRewriteSubject.

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...
676
    }
677
678
    /**
679
     * Return's the category with the passed ID.
680
     *
681
     * @param integer $categoryId    The ID of the category to return
682
     * @param string  $storeViewCode The store view code of category to return
683
     *
684
     * @return array The category data
685
     */
686 2
    protected function getCategory($categoryId, $storeViewCode = StoreViewCodes::ADMIN)
687
    {
688 2
        return $this->getSubject()->getCategory($categoryId, $storeViewCode);
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 getCategory() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Prod...jects\UrlRewriteSubject.

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...
689
    }
690
691
    /**
692
     * Persist's the URL rewrite with the passed data.
693
     *
694
     * @param array $row The URL rewrite to persist
695
     *
696
     * @return string The ID of the persisted entity
697
     */
698 3
    protected function persistUrlRewrite($row)
699
    {
700 3
        return $this->getProductUrlRewriteProcessor()->persistUrlRewrite($row);
701
    }
702
703
    /**
704
     * Persist's the URL rewrite product => category relation with the passed data.
705
     *
706
     * @param array $row The URL rewrite product => category relation to persist
707
     *
708
     * @return void
709
     */
710 2
    protected function persistUrlRewriteProductCategory($row)
711
    {
712 2
        return $this->getProductUrlRewriteProcessor()->persistUrlRewriteProductCategory($row);
713
    }
714
715
    /**
716
     * Queries whether or not the passed SKU and store view code has already been processed.
717
     *
718
     * @param string $sku           The SKU to check been processed
719
     * @param string $storeViewCode The store view code to check been processed
720
     *
721
     * @return boolean TRUE if the SKU and store view code has been processed, else FALSE
722
     */
723 3
    protected function storeViewHasBeenProcessed($sku, $storeViewCode)
724
    {
725 3
        return $this->getSubject()->storeViewHasBeenProcessed($sku, $storeViewCode);
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 storeViewHasBeenProcessed() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Prod...jects\UrlRewriteSubject.

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...
726
    }
727
728
    /**
729
     * Add the entity ID => visibility mapping for the actual entity ID.
730
     *
731
     * @param string $visibility The visibility of the actual entity to map
732
     *
733
     * @return void
734
     */
735 3
    protected function addEntityIdVisibilityIdMapping($visibility)
736
    {
737 3
        $this->getSubject()->addEntityIdVisibilityIdMapping($visibility);
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 addEntityIdVisibilityIdMapping() does only exist in the following implementations of said interface: TechDivision\Import\Prod...jects\UrlRewriteSubject.

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...
738 3
    }
739
740
    /**
741
     * Set's the ID of the product that has been created recently.
742
     *
743
     * @param string $lastEntityId The entity ID
744
     *
745
     * @return void
746
     */
747 3
    protected function setLastEntityId($lastEntityId)
748
    {
749 3
        $this->getSubject()->setLastEntityId($lastEntityId);
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 setLastEntityId() does only exist in the following implementations of said interface: TechDivision\Import\Plugins\ExportableSubjectImpl, TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Prod...jects\UrlRewriteSubject, TechDivision\Import\Subjects\ExportableTraitImpl.

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...
750 3
    }
751
752
    /**
753
     * Load's and return's the product with the passed SKU.
754
     *
755
     * @param string $sku The SKU of the product to load
756
     *
757
     * @return array The product
758
     */
759 3
    protected function loadProduct($sku)
760
    {
761 3
        return $this->getProductUrlRewriteProcessor()->loadProduct($sku);
762
    }
763
}
764