11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2015 Adobe
4+ * All Rights Reserved .
55 */
6+
67declare (strict_types=1 );
78
89namespace Magento \Store \Model ;
910
11+ use Magento \Framework \App \ObjectManager ;
12+ use Magento \Framework \App \Request \Http ;
13+ use Magento \Framework \Cache \FrontendInterface ;
14+ use Magento \Framework \Exception \NoSuchEntityException ;
15+ use Magento \Store \Api \Data \StoreInterface ;
16+ use Magento \Store \Api \StoreCookieManagerInterface ;
17+ use Magento \Store \Api \StoreRepositoryInterface ;
18+ use Magento \Store \Api \StoreResolverInterface ;
19+ use Magento \Store \App \Request \StorePathInfoValidator ;
20+ use Magento \Framework \Stdlib \CookieManagerInterface ;
21+
1022/**
1123 * Class used to resolve store from url path or get parameters or cookie.
24+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1226 */
13- class StoreResolver implements \ Magento \ Store \ Api \ StoreResolverInterface
27+ class StoreResolver implements StoreResolverInterface
1428{
1529 /**
1630 * Cache tag
1731 */
18- const CACHE_TAG = 'store_relations ' ;
32+ public const CACHE_TAG = 'store_relations ' ;
1933
2034 /**
21- * @var \Magento\Store\Api\ StoreRepositoryInterface
35+ * @var StoreRepositoryInterface
2236 */
2337 protected $ storeRepository ;
2438
2539 /**
26- * @var \Magento\Store\Api\ StoreCookieManagerInterface
40+ * @var StoreCookieManagerInterface
2741 */
2842 protected $ storeCookieManager ;
2943
3044 /**
3145 * @deprecated 101.0.0
46+ * @see No longer needed
47+ *
48+ * @var FrontendInterface
3249 */
3350 protected $ cache ;
3451
3552 /**
3653 * @deprecated 101.0.0
54+ * @see No longer needed
55+ *
56+ * @var StoreResolver\ReaderList
3757 */
3858 protected $ readerList ;
3959
@@ -48,7 +68,7 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface
4868 protected $ scopeCode ;
4969
5070 /**
51- * @var \Magento\Framework\App\Request\ Http
71+ * @var Http
5272 */
5373 protected $ request ;
5474
@@ -58,27 +78,34 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface
5878 private $ storesData ;
5979
6080 /**
61- * @var \Magento\Store\App\Request\ StorePathInfoValidator
81+ * @var StorePathInfoValidator
6282 */
6383 private $ storePathInfoValidator ;
6484
6585 /**
66- * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
67- * @param \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager
68- * @param \Magento\Framework\App\Request\Http $request
69- * @param \Magento\Store\Model\StoresData $storesData
70- * @param \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator
71- * @param string|null $runMode
86+ * @var CookieManagerInterface
87+ */
88+ private $ cookieManagerInterface ;
89+
90+ /**
91+ * @param StoreRepositoryInterface $storeRepository
92+ * @param StoreCookieManagerInterface $storeCookieManager
93+ * @param Http $request
94+ * @param StoresData $storesData
95+ * @param StorePathInfoValidator $storePathInfoValidator
96+ * @param string $runMode
7297 * @param string|null $scopeCode
98+ * @param CookieManagerInterface|null $cookieManagerInterface
7399 */
74100 public function __construct (
75- \ Magento \ Store \ Api \ StoreRepositoryInterface $ storeRepository ,
76- \ Magento \ Store \ Api \ StoreCookieManagerInterface $ storeCookieManager ,
77- \ Magento \ Framework \ App \ Request \ Http $ request ,
78- \ Magento \ Store \ Model \ StoresData $ storesData ,
79- \ Magento \ Store \ App \ Request \ StorePathInfoValidator $ storePathInfoValidator ,
101+ StoreRepositoryInterface $ storeRepository ,
102+ StoreCookieManagerInterface $ storeCookieManager ,
103+ Http $ request ,
104+ StoresData $ storesData ,
105+ StorePathInfoValidator $ storePathInfoValidator ,
80106 $ runMode = ScopeInterface::SCOPE_STORE ,
81- $ scopeCode = null
107+ $ scopeCode = null ,
108+ ?CookieManagerInterface $ cookieManagerInterface = null
82109 ) {
83110 $ this ->storeRepository = $ storeRepository ;
84111 $ this ->storeCookieManager = $ storeCookieManager ;
@@ -87,6 +114,8 @@ public function __construct(
87114 $ this ->storesData = $ storesData ;
88115 $ this ->runMode = $ scopeCode ? $ runMode : ScopeInterface::SCOPE_WEBSITE ;
89116 $ this ->scopeCode = $ scopeCode ;
117+ $ this ->cookieManagerInterface = $ cookieManagerInterface ?:
118+ ObjectManager::getInstance ()->get (CookieManagerInterface::class);
90119 }
91120
92121 /**
@@ -95,12 +124,11 @@ public function __construct(
95124 public function getCurrentStoreId ()
96125 {
97126 list ($ stores , $ defaultStoreId ) = $ this ->getStoresData ();
98-
99127 $ storeCode = $ this ->storePathInfoValidator ->getValidStoreCode ($ this ->request );
100128
101129 if (!$ storeCode ) {
102130 $ storeCode = $ this ->request ->getParam (
103- \ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ,
131+ StoreManagerInterface::PARAM_NAME ,
104132 $ this ->storeCookieManager ->getStoreCodeFromCookie ()
105133 );
106134 }
@@ -115,7 +143,9 @@ public function getCurrentStoreId()
115143 if ($ storeCode ) {
116144 try {
117145 $ store = $ this ->getRequestedStoreByCode ($ storeCode );
118- } catch (\Magento \Framework \Exception \NoSuchEntityException $ e ) {
146+ } catch (NoSuchEntityException $ e ) {
147+ $ this ->request ->setQueryValue (StoreManagerInterface::PARAM_NAME );
148+ $ this ->cookieManagerInterface ->deleteCookie (StoreCookieManager::COOKIE_NAME );
119149 $ store = $ this ->getDefaultStoreById ($ defaultStoreId );
120150 }
121151
@@ -143,7 +173,7 @@ protected function getStoresData() : array
143173 *
144174 * @return array
145175 * @deprecated 101.0.0
146- * @see \Magento\Store\Model\ StoreResolver::getStoresData
176+ * @see StoreResolver::getStoresData
147177 */
148178 protected function readStoresData () : array
149179 {
@@ -154,15 +184,15 @@ protected function readStoresData() : array
154184 * Retrieve active store by code
155185 *
156186 * @param string $storeCode
157- * @return \Magento\Store\Api\Data\ StoreInterface
158- * @throws \Magento\Framework\Exception\ NoSuchEntityException
187+ * @return StoreInterface
188+ * @throws NoSuchEntityException
159189 */
160- protected function getRequestedStoreByCode ($ storeCode ) : \ Magento \ Store \ Api \ Data \ StoreInterface
190+ protected function getRequestedStoreByCode ($ storeCode ) : StoreInterface
161191 {
162192 try {
163193 $ store = $ this ->storeRepository ->getActiveStoreByCode ($ storeCode );
164194 } catch (StoreIsInactiveException $ e ) {
165- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (__ ('Requested store is inactive ' ));
195+ throw new NoSuchEntityException (__ ('Requested store is inactive ' ));
166196 }
167197
168198 return $ store ;
@@ -172,15 +202,15 @@ protected function getRequestedStoreByCode($storeCode) : \Magento\Store\Api\Data
172202 * Retrieve active store by code
173203 *
174204 * @param int $id
175- * @return \Magento\Store\Api\Data\ StoreInterface
176- * @throws \Magento\Framework\Exception\ NoSuchEntityException
205+ * @return StoreInterface
206+ * @throws NoSuchEntityException
177207 */
178- protected function getDefaultStoreById ($ id ) : \ Magento \ Store \ Api \ Data \ StoreInterface
208+ protected function getDefaultStoreById ($ id ) : StoreInterface
179209 {
180210 try {
181211 $ store = $ this ->storeRepository ->getActiveStoreById ($ id );
182212 } catch (StoreIsInactiveException $ e ) {
183- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (__ ('Default store is inactive ' ));
213+ throw new NoSuchEntityException (__ ('Default store is inactive ' ));
184214 }
185215
186216 return $ store ;
0 commit comments