-
Notifications
You must be signed in to change notification settings - Fork 4
/
UrlKeyAndPathObserver.php
243 lines (218 loc) · 8.59 KB
/
UrlKeyAndPathObserver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* TechDivision\Import\Category\Observers\UrlKeyAndPathObserver
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* PHP version 5
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2019 TechDivision GmbH <info@techdivision.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/techdivision/import-category
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Category\Observers;
use Zend\Filter\FilterInterface;
use TechDivision\Import\Category\Utils\ColumnKeys;
use TechDivision\Import\Category\Utils\MemberNames;
use TechDivision\Import\Utils\UrlKeyUtilInterface;
use TechDivision\Import\Utils\Filter\UrlKeyFilterTrait;
use TechDivision\Import\Utils\StoreViewCodes;
use TechDivision\Import\Category\Services\CategoryBunchProcessorInterface;
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface;
/**
* Observer that extracts the URL key/path from the category path
* and adds them as two new columns with the their values.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2019 TechDivision GmbH <info@techdivision.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/techdivision/import-category
* @link http://www.techdivision.com
*/
class UrlKeyAndPathObserver extends AbstractCategoryImportObserver
{
/**
* The trait that provides string => URL key conversion functionality.
*
* @var \TechDivision\Import\Utils\Filter\UrlKeyFilterTrait
*/
use UrlKeyFilterTrait;
/**
* The URL key utility instance.
*
* @var \TechDivision\Import\Utils\UrlKeyUtilInterface
*/
protected $urlKeyUtil;
/**
* The category bunch processor instance.
*
* @var \TechDivision\Import\Category\Services\CategoryBunchProcessorInterface
*/
protected $categoryBunchProcessor;
/**
* Initialize the observer with the passed product bunch processor instance.
*
* @param \TechDivision\Import\Category\Services\CategoryBunchProcessorInterface $categoryBunchProcessor The category bunch processor instance
* @param \Zend\Filter\FilterInterface $convertLiteralUrlFilter The URL filter instance
* @param \TechDivision\Import\Utils\UrlKeyUtilInterface $urlKeyUtil The URL key utility instance
*/
public function __construct(
CategoryBunchProcessorInterface $categoryBunchProcessor,
FilterInterface $convertLiteralUrlFilter,
UrlKeyUtilInterface $urlKeyUtil
) {
// set the processor and the URL filter instance
$this->categoryBunchProcessor = $categoryBunchProcessor;
$this->convertLiteralUrlFilter = $convertLiteralUrlFilter;
$this->urlKeyUtil = $urlKeyUtil;
}
/**
* Process the observer's business logic.
*
* @return void
*/
protected function process()
{
// initialize the URL key and array for the categories
$urlKey = null;
$categories = array();
// set the entity ID for the category with the passed path
try {
$this->setIds($this->getCategoryByPath($this->getValue(ColumnKeys::PATH)));
} catch (\Exception $e) {
$this->setIds(array());
}
// query whether or not the URL key column has a value
if ($this->hasValue(ColumnKeys::URL_KEY)) {
$urlKey = $this->makeUnique($this->getSubject(), $this->getValue(ColumnKeys::URL_KEY));
} else {
$this->setValue(
ColumnKeys::URL_KEY,
$urlKey = $this->makeUnique($this->getSubject(), $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME)))
);
}
// explode the path into the category names
if ($categories = $this->explode($this->getValue(ColumnKeys::PATH), '/')) {
// initialize the category with the actual category's URL key
$categoryPaths = array($urlKey);
// prepare the store view code
$this->prepareStoreViewCode();
// load ID of the actual store view
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
// iterate over the category names and try to load the category therefore
for ($i = sizeof($categories) - 1; $i > 1; $i--) {
try {
// prepare the expected category name
$categoryPath = implode('/', array_slice($categories, 0, $i));
// load the existing category and prepend the URL key the array with the category URL keys
$existingCategory = $this->getCategoryByPkAndStoreId($this->mapPath($categoryPath), $storeId);
// query whether or not an URL key is available or not
if (isset($existingCategory[MemberNames::URL_KEY])) {
array_unshift($categoryPaths, $existingCategory[MemberNames::URL_KEY]);
} else {
$this->getSystemLogger()->debug(sprintf('Can\'t find URL key for category %s', $categoryPath));
}
} catch (\Exception $e) {
$this->getSystemLogger()->debug(sprintf('Can\'t load parent category %s', $categoryPath));
}
}
// create the header for the URL path
if (!$this->hasHeader(ColumnKeys::URL_PATH)) {
$this->addHeader(ColumnKeys::URL_PATH);
}
// set the URL path
$this->setValue(ColumnKeys::URL_PATH, implode('/', $categoryPaths));
}
}
/**
* Return the primary key member name.
*
* @return string The primary key member name
*/
protected function getPkMemberName()
{
return MemberNames::ENTITY_ID;
}
/**
* Returns the category bunch processor instance.
*
* @return \TechDivision\Import\Category\Services\CategoryBunchProcessorInterface The category bunch processor instance
*/
protected function getCategoryBunchProcessor()
{
return $this->categoryBunchProcessor;
}
/**
* Returns the URL key utility instance.
*
* @return \TechDivision\Import\Utils\UrlKeyUtilInterface The URL key utility instance
*/
protected function getUrlKeyUtil()
{
return $this->urlKeyUtil;
}
/**
* Return's the category with the passed path.
*
* @param string $path The path of the category to return
*
* @return array The category
* @throws \Exception Is thrown, if the requested category is not available
*/
protected function getCategoryByPath($path)
{
return $this->getSubject()->getCategoryByPath($path);
}
/**
* Returns the category with the passed primary key and the attribute values for the passed store ID.
*
* @param string $pk The primary key of the category to return
* @param integer $storeId The store ID of the category values
*
* @return array|null The category data
*/
protected function getCategoryByPkAndStoreId($pk, $storeId)
{
return $this->getCategoryBunchProcessor()->getCategoryByPkAndStoreId($pk, $storeId);
}
/**
* Temporarily persist's the IDs of the passed category.
*
* @param array $category The category to temporarily persist the IDs for
*
* @return void
*/
protected function setIds(array $category)
{
$this->setLastEntityId(isset($category[$this->getPkMemberName()]) ? $category[$this->getPkMemberName()] : null);
}
/**
* Set's the ID of the category that has been created recently.
*
* @param string $lastEntityId The entity ID
*
* @return void
*/
protected function setLastEntityId($lastEntityId)
{
$this->getSubject()->setLastEntityId($lastEntityId);
}
/**
* Make's the passed URL key unique by adding the next number to the end.
*
* @param \TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface $subject The subject to make the URL key unique for
* @param string $urlKey The URL key to make unique
*
* @return string The unique URL key
*/
protected function makeUnique(UrlKeyAwareSubjectInterface $subject, $urlKey)
{
return $this->getUrlKeyUtil()->makeUnique($subject, $urlKey);
}
}