7
7
namespace Magento \Cms \Model ;
8
8
9
9
use Magento \Cms \Api \Data ;
10
+ use Magento \Cms \Api \Data \PageInterface ;
11
+ use Magento \Cms \Api \Data \PageInterfaceFactory ;
12
+ use Magento \Cms \Api \Data \PageSearchResultsInterface ;
10
13
use Magento \Cms \Api \PageRepositoryInterface ;
14
+ use Magento \Cms \Model \Api \SearchCriteria \PageCollectionProcessor ;
11
15
use Magento \Cms \Model \Page \IdentityMap ;
16
+ use Magento \Cms \Model \ResourceModel \Page as ResourcePage ;
17
+ use Magento \Cms \Model \ResourceModel \Page \CollectionFactory as PageCollectionFactory ;
12
18
use Magento \Framework \Api \DataObjectHelper ;
13
19
use Magento \Framework \Api \SearchCriteria \CollectionProcessorInterface ;
20
+ use Magento \Framework \Api \SearchCriteriaInterface ;
14
21
use Magento \Framework \App \ObjectManager ;
22
+ use Magento \Framework \App \Route \Config ;
15
23
use Magento \Framework \Exception \CouldNotDeleteException ;
16
24
use Magento \Framework \Exception \CouldNotSaveException ;
17
25
use Magento \Framework \Exception \NoSuchEntityException ;
18
26
use Magento \Framework \Reflection \DataObjectProcessor ;
19
- use Magento \Cms \Model \ResourceModel \Page as ResourcePage ;
20
- use Magento \Cms \Model \ResourceModel \Page \CollectionFactory as PageCollectionFactory ;
21
27
use Magento \Store \Model \StoreManagerInterface ;
22
28
23
29
/**
24
- * Class PageRepository
30
+ * Cms page repository
25
31
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
32
*/
27
33
class PageRepository implements PageRepositoryInterface
@@ -57,12 +63,12 @@ class PageRepository implements PageRepositoryInterface
57
63
protected $ dataObjectProcessor ;
58
64
59
65
/**
60
- * @var \Magento\Cms\Api\Data\ PageInterfaceFactory
66
+ * @var PageInterfaceFactory
61
67
*/
62
68
protected $ dataPageFactory ;
63
69
64
70
/**
65
- * @var \Magento\Store\Model\ StoreManagerInterface
71
+ * @var StoreManagerInterface
66
72
*/
67
73
private $ storeManager ;
68
74
@@ -76,30 +82,37 @@ class PageRepository implements PageRepositoryInterface
76
82
*/
77
83
private $ identityMap ;
78
84
85
+ /**
86
+ * @var Config
87
+ */
88
+ private $ routeConfig ;
89
+
79
90
/**
80
91
* @param ResourcePage $resource
81
92
* @param PageFactory $pageFactory
82
- * @param Data\ PageInterfaceFactory $dataPageFactory
93
+ * @param PageInterfaceFactory $dataPageFactory
83
94
* @param PageCollectionFactory $pageCollectionFactory
84
95
* @param Data\PageSearchResultsInterfaceFactory $searchResultsFactory
85
96
* @param DataObjectHelper $dataObjectHelper
86
97
* @param DataObjectProcessor $dataObjectProcessor
87
98
* @param StoreManagerInterface $storeManager
88
99
* @param CollectionProcessorInterface $collectionProcessor
89
100
* @param IdentityMap|null $identityMap
101
+ * @param Config|null $routeConfig
90
102
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
91
103
*/
92
104
public function __construct (
93
105
ResourcePage $ resource ,
94
106
PageFactory $ pageFactory ,
95
- Data \ PageInterfaceFactory $ dataPageFactory ,
107
+ PageInterfaceFactory $ dataPageFactory ,
96
108
PageCollectionFactory $ pageCollectionFactory ,
97
109
Data \PageSearchResultsInterfaceFactory $ searchResultsFactory ,
98
110
DataObjectHelper $ dataObjectHelper ,
99
111
DataObjectProcessor $ dataObjectProcessor ,
100
112
StoreManagerInterface $ storeManager ,
101
113
CollectionProcessorInterface $ collectionProcessor = null ,
102
- ?IdentityMap $ identityMap = null
114
+ ?IdentityMap $ identityMap = null ,
115
+ ?Config $ routeConfig = null
103
116
) {
104
117
$ this ->resource = $ resource ;
105
118
$ this ->pageFactory = $ pageFactory ;
@@ -110,17 +123,20 @@ public function __construct(
110
123
$ this ->dataObjectProcessor = $ dataObjectProcessor ;
111
124
$ this ->storeManager = $ storeManager ;
112
125
$ this ->collectionProcessor = $ collectionProcessor ?: $ this ->getCollectionProcessor ();
113
- $ this ->identityMap = $ identityMap ?? ObjectManager::getInstance ()->get (IdentityMap::class);
126
+ $ this ->identityMap = $ identityMap ?? ObjectManager::getInstance ()
127
+ ->get (IdentityMap::class);
128
+ $ this ->routeConfig = $ routeConfig ?? ObjectManager::getInstance ()
129
+ ->get (Config::class);
114
130
}
115
131
116
132
/**
117
133
* Validate new layout update values.
118
134
*
119
- * @param Data\ PageInterface $page
135
+ * @param PageInterface $page
120
136
* @return void
121
137
* @throws \InvalidArgumentException
122
138
*/
123
- private function validateLayoutUpdate (Data \ PageInterface $ page ): void
139
+ private function validateLayoutUpdate (PageInterface $ page ): void
124
140
{
125
141
//Persisted data
126
142
$ savedPage = $ page ->getId () ? $ this ->getById ($ page ->getId ()) : null ;
@@ -140,18 +156,19 @@ private function validateLayoutUpdate(Data\PageInterface $page): void
140
156
/**
141
157
* Save Page data
142
158
*
143
- * @param \Magento\Cms\Api\Data\ PageInterface|Page $page
159
+ * @param PageInterface|Page $page
144
160
* @return Page
145
161
* @throws CouldNotSaveException
146
162
*/
147
- public function save (\ Magento \ Cms \ Api \ Data \ PageInterface $ page )
163
+ public function save (PageInterface $ page )
148
164
{
149
165
if ($ page ->getStoreId () === null ) {
150
166
$ storeId = $ this ->storeManager ->getStore ()->getId ();
151
167
$ page ->setStoreId ($ storeId );
152
168
}
153
169
try {
154
170
$ this ->validateLayoutUpdate ($ page );
171
+ $ this ->validateRoutesDuplication ($ page );
155
172
$ this ->resource ->save ($ page );
156
173
$ this ->identityMap ->add ($ page );
157
174
} catch (\Exception $ exception ) {
@@ -168,7 +185,7 @@ public function save(\Magento\Cms\Api\Data\PageInterface $page)
168
185
*
169
186
* @param string $pageId
170
187
* @return Page
171
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
188
+ * @throws NoSuchEntityException
172
189
*/
173
190
public function getById ($ pageId )
174
191
{
@@ -187,17 +204,15 @@ public function getById($pageId)
187
204
*
188
205
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
189
206
* @SuppressWarnings(PHPMD.NPathComplexity)
190
- * @param \Magento\Framework\Api\ SearchCriteriaInterface $criteria
191
- * @return \Magento\Cms\Api\Data\ PageSearchResultsInterface
207
+ * @param SearchCriteriaInterface $criteria
208
+ * @return PageSearchResultsInterface
192
209
*/
193
- public function getList (\ Magento \ Framework \ Api \ SearchCriteriaInterface $ criteria )
210
+ public function getList (SearchCriteriaInterface $ criteria )
194
211
{
195
- /** @var \Magento\Cms\Model\ResourceModel\Page\Collection $collection */
196
212
$ collection = $ this ->pageCollectionFactory ->create ();
197
213
198
214
$ this ->collectionProcessor ->process ($ criteria , $ collection );
199
215
200
- /** @var Data\PageSearchResultsInterface $searchResults */
201
216
$ searchResults = $ this ->searchResultsFactory ->create ();
202
217
$ searchResults ->setSearchCriteria ($ criteria );
203
218
$ searchResults ->setItems ($ collection ->getItems ());
@@ -208,11 +223,11 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria
208
223
/**
209
224
* Delete Page
210
225
*
211
- * @param \Magento\Cms\Api\Data\ PageInterface $page
226
+ * @param PageInterface $page
212
227
* @return bool
213
228
* @throws CouldNotDeleteException
214
229
*/
215
- public function delete (\ Magento \ Cms \ Api \ Data \ PageInterface $ page )
230
+ public function delete (PageInterface $ page )
216
231
{
217
232
try {
218
233
$ this ->resource ->delete ($ page );
@@ -247,10 +262,26 @@ public function deleteById($pageId)
247
262
private function getCollectionProcessor ()
248
263
{
249
264
if (!$ this ->collectionProcessor ) {
250
- $ this -> collectionProcessor = \ Magento \Framework \ App \ObjectManager:: getInstance ()-> get (
251
- \ Magento \ Cms \ Model \ Api \ SearchCriteria \PageCollectionProcessor::class
252
- );
265
+ // phpstan:ignore "Class Magento\Cms\Model\Api\SearchCriteria\PageCollectionProcessor not found."
266
+ $ this -> collectionProcessor = ObjectManager:: getInstance ()
267
+ -> get (PageCollectionProcessor::class );
253
268
}
254
269
return $ this ->collectionProcessor ;
255
270
}
271
+
272
+ /**
273
+ * Checks that page identifier doesn't duplicate existed routes
274
+ *
275
+ * @param PageInterface $page
276
+ * @return void
277
+ * @throws CouldNotSaveException
278
+ */
279
+ private function validateRoutesDuplication ($ page ): void
280
+ {
281
+ if ($ this ->routeConfig ->getRouteByFrontName ($ page ->getIdentifier (), 'frontend ' )) {
282
+ throw new CouldNotSaveException (
283
+ __ ('The value specified in the URL Key field would generate a URL that already exists. ' )
284
+ );
285
+ }
286
+ }
256
287
}
0 commit comments