Test Failed
Pull Request — master (#1)
by Tibor
02:28
created

CategoryUrlPathValidatorObserver::execute()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 31
Code Lines 20

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 20
nc 6
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\Framework\Exception\LocalizedException;
9
use Magento\Store\Model\StoreManagerInterface;
10
use Tkotosz\CatalogRouter\Api\CatalogUrlPathProviderInterface;
11
use Tkotosz\CatalogRouter\Api\ProductResolverInterface;
12
use Tkotosz\CatalogRouter\Model\EntityData;
13
use Tkotosz\CatalogRouter\Model\Service\UrlPathUsedCheckerContainer;
14
use Tkotosz\CatalogRouter\Model\UrlPath;
15
16 View Code Duplication
class CategoryUrlPathValidatorObserver 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...
17
{
18
    /**
19
     * @var UrlPathUsedCheckerContainer
20
     */
21
    private $urlPathUsedChecker;
22
    
23
    /**
24
     * @var StoreManagerInterface
25
     */
26
    private $storeManager;
27
    
28
    /**
29
     * @var CatalogUrlPathProviderInterface
30
     */
31
    private $urlPathProvider;
32
    
33
    /**
34
     * @param UrlPathUsedCheckerContainer     $urlPathUsedChecker
35
     * @param StoreManagerInterface           $storeManager
36
     * @param CatalogUrlPathProviderInterface $urlPathProvider
37
     */
38
    public function __construct(UrlPathUsedCheckerContainer $urlPathUsedChecker, StoreManagerInterface $storeManager, CatalogUrlPathProviderInterface $urlPathProvider)
39
    {
40
        $this->urlPathUsedChecker = $urlPathUsedChecker;
41
        $this->storeManager = $storeManager;
42
        $this->urlPathProvider = $urlPathProvider;
43
    }
44
    
45
    /**
46
     * @param Observer $observer
47
     * @return void
48
     */
49
    public function execute(Observer $observer)
50
    {
51
        /** @var Category $category */
52
        $category = $observer->getEvent()->getCategory();
53
54
        foreach ($category->getStoreIds() as $storeId) {
55
            if (!$storeId) continue;
56
            $urlPath = $this->urlPathProvider->getCategoryUrlPath($category->getId(), $storeId);
57
            $resolvedEntities = $this->urlPathUsedChecker->check($urlPath, $storeId);
58
59
            if (count($resolvedEntities) > 1) {
60
                $store = $this->storeManager->getStore($storeId);
61
                $messages = [];
62
                foreach ($resolvedEntities as $entity) {
63
                    if ($entity->getType() == 'category' && $entity->getId() == $category->getId()) {
64
                        continue;
65
                    }
66
                    $messages[] = __(
67
                        'The "%1" url path already used by a %2 with id %3 in the %4 (storeid: %5) store',
68
                        $urlPath->getIdentifier(),
69
                        $entity->getType(),
70
                        $entity->getId(),
71
                        $store->getName(),
72
                        $store->getId()
73
                    );
74
                }
75
76
                throw new \Magento\Framework\Exception\AlreadyExistsException(__(join("<br>", $messages)));
77
            }
78
        }
79
    }
80
}
81