Failed Conditions
Pull Request — master (#1)
by Tibor
04:33 queued 01:50
created

PathValidatorObserver::getOtherEntitiesWithPath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 3
1
<?php
2
3
namespace Tkotosz\CatalogRouter\Observer;
4
5
use Magento\Backend\Model\UrlInterface;
6
use Magento\Framework\Event\Observer;
7
use Magento\Framework\Event\ObserverInterface;
8
use Magento\Framework\Exception\AlreadyExistsException;
9
use Magento\Framework\Model\AbstractModel;
10
use Magento\Store\Model\StoreManagerInterface;
11
use Tkotosz\CatalogRouter\Api\CatalogUrlPathProviderInterface;
12
use Tkotosz\CatalogRouter\Model\EntityData;
13
use Tkotosz\CatalogRouter\Model\Service\UrlPathUsedCheckerContainer;
14
use Tkotosz\CatalogRouter\Model\UrlPath;
15
16
abstract class PathValidatorObserver implements ObserverInterface
17
{
18
    /**
19
     * @var UrlPathUsedCheckerContainer
20
     */
21
    protected $urlPathUsedChecker;
22
    
23
    /**
24
     * @var StoreManagerInterface
25
     */
26
    protected $storeManager;
27
    
28
    /**
29
     * @var CatalogUrlPathProviderInterface
30
     */
31
    protected $urlPathProvider;
32
    
33
    /**
34
     * @var UrlInterface
35
     */
36
    protected $urlProvider;
37
    
38
    /**
39
     * @var array
40
     */
41
    protected $entityEditUrls;
42
    
43
    /**
44
     * @param UrlPathUsedCheckerContainer     $urlPathUsedChecker
45
     * @param StoreManagerInterface           $storeManager
46
     * @param CatalogUrlPathProviderInterface $urlPathProvider
47
     * @param UrlInterface                    $urlProvider
48
     * @param array                           $entityEditUrls
49
     */
50
    public function __construct(
51
        UrlPathUsedCheckerContainer $urlPathUsedChecker,
52
        StoreManagerInterface $storeManager,
53
        CatalogUrlPathProviderInterface $urlPathProvider,
54
        UrlInterface $urlProvider,
55
        array $entityEditUrls
56
    ) {
57
        $this->urlPathUsedChecker = $urlPathUsedChecker;
58
        $this->storeManager = $storeManager;
59
        $this->urlPathProvider = $urlPathProvider;
60
        $this->urlProvider = $urlProvider;
61
        $this->entityEditUrls = $entityEditUrls;
62
    }
63
64
    /**
65
     * @param Observer $observer
66
     * @return void
67
     */
68
    public function execute(Observer $observer)
69
    {
70
        $entity = $this->getEntity($observer);
71
72
        foreach ($this->getEntityStoreIds($entity) as $storeId) {
73
            $urlPath = $this->getEntityUrlPath($entity, $storeId);
74
            $resolvedEntities = $this->getOtherEntitiesWithPath($urlPath, $storeId, $entity);
75
76
            if (count($resolvedEntities) > 0) {
77
                throw new AlreadyExistsException($this->getErrorMessage($urlPath, $storeId, $resolvedEntities));
78
            }
79
        }
80
    }
81
82
    protected function getOtherEntitiesWithPath(UrlPath $urlPath, int $storeId, AbstractModel $currentEntity)
83
    {
84
        $resolvedEntities = [];
85
86
        $currentEntityData = new EntityData($this->getCurrentEntityType(), $currentEntity->getId(), $urlPath->getLastPart());
87
88
        foreach ($this->urlPathUsedChecker->check($urlPath, $storeId) as $entity) {
89
            if ($entity->getType() == $currentEntityData->getType() && $entity->getId() == $currentEntityData->getId()) {
90
                continue;
91
            }
92
93
            $resolvedEntities[] = $entity;
94
        }
95
96
        return $resolvedEntities;
97
    }
98
99
    protected function getErrorMessage(UrlPath $urlPath, int $storeId, array $entities)
100
    {
101
        $messages = [];
102
103
        $store = $this->storeManager->getStore($storeId);
104
        
105
        foreach ($entities as $entity) {
106
            $editUrlPath = $this->entityEditUrls[$entity->getType()]['url_path'];
107
            $idParam = $this->entityEditUrls[$entity->getType()]['id_param'];
108
            $editUrl = $this->urlProvider->getUrl($editUrlPath, [$idParam => $entity->getId()]);
109
110
            $messages[] = __(
111
                'The "%1" url path already used by a <a href="%2" target="_blank">%3 (id: %4)</a> in the %5 (id: %6) store',
112
                $urlPath->getFullPath(),
113
                $editUrl,
114
                $entity->getType(),
115
                $entity->getId(),
116
                $store->getName(),
117
                $store->getId()
118
            );
119
        }
120
121
        return __(join("<br>", $messages));
122
    }
123
124
    abstract protected function getCurrentEntityType();
125
126
    abstract protected function getEntity(Observer $observer);
127
128
    abstract protected function getEntityStoreIds(AbstractModel $entity);
129
130
    abstract protected function getEntityUrlPath(AbstractModel $entity, int $storeId);
131
}
132