@@ -84,24 +84,9 @@ class Router implements \Magento\Framework\App\RouterInterface
8484 protected $ _response ;
8585
8686 /**
87- * @var array
87+ * @var array;
8888 */
89- protected $ _postId ;
90-
91- /**
92- * @var array
93- */
94- protected $ _categoryId ;
95-
96- /**
97- * @var int
98- */
99- protected $ _authorId ;
100-
101- /**
102- * @var int
103- */
104- protected $ _tagId ;
89+ protected $ ids ;
10590
10691 /**
10792 * @param \Magento\Framework\App\ActionFactory $actionFactory
@@ -125,7 +110,8 @@ public function __construct(
125110 \Magefan \Blog \Model \TagFactory $ tagFactory ,
126111 \Magento \Store \Model \StoreManagerInterface $ storeManager ,
127112 \Magento \Framework \App \ResponseInterface $ response
128- ) {
113+ )
114+ {
129115 $ this ->actionFactory = $ actionFactory ;
130116 $ this ->_eventManager = $ eventManager ;
131117 $ this ->_url = $ url ;
@@ -330,22 +316,12 @@ public function match(\Magento\Framework\App\RequestInterface $request)
330316 */
331317 protected function _getPostId ($ identifier , $ checkSufix = true )
332318 {
333- $ key = $ identifier . ($ checkSufix ? '-checksufix ' : '' );
334- if (!isset ($ this ->_postId [$ key ])) {
335- $ sufix = $ this ->_url ->getUrlSufix (Url::CONTROLLER_POST );
336- $ trimmedIdentifier = $ this ->_url ->trimSufix ($ identifier , $ sufix );
337- if ($ checkSufix && $ sufix && $ trimmedIdentifier == $ identifier ) { //if url without sufix
338- $ this ->_postId [$ key ] = 0 ;
339- } else {
340- $ post = $ this ->_postFactory ->create ();
341- $ this ->_postId [$ key ] = $ post ->checkIdentifier (
342- $ trimmedIdentifier ,
343- $ this ->_storeManager ->getStore ()->getId ()
344- );
345- }
346- }
347-
348- return $ this ->_postId [$ key ];
319+ return $ this ->getObjectId (
320+ $ this ->_postFactory ,
321+ Url::CONTROLLER_POST ,
322+ $ identifier ,
323+ $ checkSufix
324+ );
349325 }
350326
351327 /**
@@ -355,58 +331,73 @@ protected function _getPostId($identifier, $checkSufix = true)
355331 */
356332 protected function _getCategoryId ($ identifier , $ checkSufix = true )
357333 {
358- $ key = $ identifier . ($ checkSufix ? '-checksufix ' : '' );
359- if (!isset ($ this ->_categoryId [$ key ])) {
360- $ sufix = $ this ->_url ->getUrlSufix (Url::CONTROLLER_CATEGORY );
361-
362- $ trimmedIdentifier = $ this ->_url ->trimSufix ($ identifier , $ sufix );
363-
364- if ($ checkSufix && $ sufix && $ trimmedIdentifier == $ identifier ) { //if url without sufix
365- $ this ->_categoryId [$ key ] = 0 ;
366- } else {
367- $ category = $ this ->_categoryFactory ->create ();
368- $ this ->_categoryId [$ key ] = $ category ->checkIdentifier (
369- $ trimmedIdentifier ,
370- $ this ->_storeManager ->getStore ()->getId ()
371- );
372- }
373- }
374-
375- return $ this ->_categoryId [$ key ];
334+ return $ this ->getObjectId (
335+ $ this ->_categoryFactory ,
336+ Url::CONTROLLER_CATEGORY ,
337+ $ identifier ,
338+ $ checkSufix
339+ );
376340 }
377341
378342 /**
379343 * Retrieve category id by identifier
380- * @param string $identifier
344+ * @param string $identifier
345+ * @param bool $checkSufix
381346 * @return int
382347 */
383- protected function _getAuthorId ($ identifier )
348+ protected function _getAuthorId ($ identifier, $ checkSufix = true )
384349 {
385- if (is_null ($ this ->_authorId )) {
386- $ author = $ this ->_authorFactory ->create ();
387- $ this ->_authorId = $ author ->checkIdentifier (
388- $ identifier
389- );
390- }
391-
392- return $ this ->_authorId ;
350+ return $ this ->getObjectId (
351+ $ this ->_authorFactory ,
352+ Url::CONTROLLER_AUTHOR ,
353+ $ identifier ,
354+ $ checkSufix
355+ );
393356 }
394357
395358 /**
396359 * Retrieve tag id by identifier
397- * @param string $identifier
360+ * @param string $identifier
361+ * @param bool $checkSufix
398362 * @return int
399363 */
400- protected function _getTagId ($ identifier )
364+ protected function _getTagId ($ identifier, $ checkSufix = true )
401365 {
402- if (is_null ($ this ->_tagId )) {
403- $ tag = $ this ->_tagFactory ->create ();
404- $ this ->_tagId = $ tag ->checkIdentifier (
405- $ identifier
406- );
366+ return $ this ->getObjectId (
367+ $ this ->_tagFactory ,
368+ Url::CONTROLLER_TAG ,
369+ $ identifier ,
370+ $ checkSufix
371+ );
372+ }
373+
374+ /**
375+ * @param $factory
376+ * @param string $controllerName
377+ * @param string $identifier
378+ * @param bool $checkSufix
379+ * @return mixed
380+ */
381+ protected function getObjectId ($ factory , $ controllerName , $ identifier , $ checkSufix )
382+ {
383+ $ key = $ controllerName . '- ' .$ identifier . ($ checkSufix ? '-checksufix ' : '' );
384+ if (!isset ($ this ->ids [$ key ])) {
385+ $ sufix = $ this ->_url ->getUrlSufix ($ controllerName );
386+
387+ $ trimmedIdentifier = $ this ->_url ->trimSufix ($ identifier , $ sufix );
388+
389+ if ($ checkSufix && $ sufix && $ trimmedIdentifier == $ identifier ) { //if url without sufix
390+ $ this ->ids [$ key ] = 0 ;
391+ } else {
392+ $ object = $ factory ->create ();
393+ $ this ->ids [$ key ] = $ object ->checkIdentifier (
394+ $ trimmedIdentifier ,
395+ $ this ->_storeManager ->getStore ()->getId ()
396+ );
397+ }
407398 }
408399
409- return $ this ->_tagId ;
400+ return $ this ->ids [ $ key ] ;
410401 }
411402
412403 /**
0 commit comments