Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UrlRewriteExceptionMessageFactory.php
Go to the documentation of this file.
1 <?php
8 
14 
16 {
17  const URL_DUPLICATE_MESSAGE_MAP_ID = 'urlDuplicateMessageMapId';
18 
22  private $messageFactory;
23 
27  private $urlBuilder;
28 
33  public function __construct(Factory $messageFactory, \Magento\Framework\UrlInterface $urlBuilder)
34  {
35  $this->messageFactory = $messageFactory;
36  $this->urlBuilder = $urlBuilder;
37  }
38 
42  public function createMessage(\Exception $exception, $type = MessageInterface::TYPE_ERROR)
43  {
44  if ($exception instanceof UrlAlreadyExistsException) {
45  $generatedUrls = [];
46  $urls = $exception->getUrls();
47  if ($urls && is_array($urls)) {
48  foreach ($urls as $id => $url) {
49  $adminEditUrl = $this->urlBuilder->getUrl(
50  'adminhtml/url_rewrite/edit',
51  ['id' => $id]
52  );
53  $generatedUrls[$adminEditUrl] = $url['request_path'];
54  }
55  }
56  return $this->messageFactory->create($type)
57  ->setIdentifier(self::URL_DUPLICATE_MESSAGE_MAP_ID)
58  ->setText($exception->getMessage())
59  ->setData(['urls' => $generatedUrls]);
60  }
61  throw new RuntimeException(
62  __('Exception instance doesn\'t match %1 type', UrlAlreadyExistsException::class)
63  );
64  }
65 }
$id
Definition: fieldset.phtml:14
createMessage(\Exception $exception, $type=MessageInterface::TYPE_ERROR)
__()
Definition: __.php:13
$type
Definition: item.phtml:13
__construct(Factory $messageFactory, \Magento\Framework\UrlInterface $urlBuilder)
$messageFactory
Definition: messages.php:10