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

ProductUrlPathValidatorObserver::execute()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 19

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
dl 30
loc 30
c 0
b 0
f 0
rs 8.439
cc 6
eloc 19
nc 5
nop 1
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