Failed Conditions
Pull Request — master (#1)
by Tibor
02:27
created

ProductUrlPathValidatorObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 67
loc 67
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
B execute() 30 30 6

How to fix   Duplicated Code   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tkotosz\CatalogRouter\Observer;
4
5
use Magento\Catalog\Model\Product;
6
use Magento\Framework\Event\Observer;
7
use Magento\Framework\Event\ObserverInterface;
8
use Magento\Store\Model\StoreManagerInterface;
9
use Tkotosz\CatalogRouter\Api\CatalogUrlPathProviderInterface;
10
use Tkotosz\CatalogRouter\Api\ProductResolverInterface;
11
use Tkotosz\CatalogRouter\Model\EntityData;
12
use Tkotosz\CatalogRouter\Model\Service\UrlPathUsedCheckerContainer;
13
use Tkotosz\CatalogRouter\Model\UrlPath;
14
15 View Code Duplication
class ProductUrlPathValidatorObserver implements ObserverInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    /**
18
     * @var UrlPathUsedCheckerContainer
19
     */
20
    private $urlPathUsedChecker;
21
    
22
    /**
23
     * @var StoreManagerInterface
24
     */
25
    private $storeManager;
26
    
27
    /**
28
     * @var CatalogUrlPathProviderInterface
29
     */
30
    private $urlPathProvider;
31
    
32
    /**
33
     * @param UrlPathUsedCheckerContainer     $urlPathUsedChecker
34
     * @param StoreManagerInterface           $storeManager
35
     * @param CatalogUrlPathProviderInterface $urlPathProvider
36
     */
37
    public function __construct(
38
        UrlPathUsedCheckerContainer $urlPathUsedChecker,
39
        StoreManagerInterface $storeManager,
40
        CatalogUrlPathProviderInterface $urlPathProvider
41
    ) {
42
        $this->urlPathUsedChecker = $urlPathUsedChecker;
43
        $this->storeManager = $storeManager;
44
        $this->urlPathProvider = $urlPathProvider;
45
    }
46
    
47
    /**
48
     * @param Observer $observer
49
     * @return void
50
     */
51
    public function execute(Observer $observer)
52
    {
53
        /** @var Product $product */
54
        $product = $observer->getEvent()->getProduct();
55
56
        foreach ($product->getStoreIds() as $storeId) {
57
            $urlPath = $this->urlPathProvider->getProductUrlPath($product->getId(), $storeId);
58
            $resolvedEntities = $this->urlPathUsedChecker->check($urlPath, $storeId);
59
60
            if (count($resolvedEntities) > 1) {
61
                $store = $this->storeManager->getStore($storeId);
62
                $messages = [];
63
                foreach ($resolvedEntities as $entity) {
64
                    if ($entity->getType() == 'product' && $entity->getId() == $product->getId()) {
65
                        continue;
66
                    }
67
                    $messages[] = __(
68
                        'The "%1" url path already used by a %2 with id %3 in the %4 (storeid: %5) store',
69
                        $urlPath->getIdentifier(),
70
                        $entity->getType(),
71
                        $entity->getId(),
72
                        $store->getName(),
73
                        $store->getId()
74
                    );
75
                }
76
77
                throw new \Exception(join("<br>", $messages));
78
            }
79
        }
80
    }
81
}
82