Completed
Pull Request — master (#62)
by Tim
03:06
created

UrlRewriteObserver::convertNameToUrlKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\UrlRewriteObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Product\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Utils\CoreConfigDataKeys;
26
use TechDivision\Import\Utils\Filter\ConvertLiteralUrl;
27
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
28
use TechDivision\Import\Utils\StoreViewCodes;
29
30
/**
31
 * Observer that creates/updates the product's URL rewrites.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product
37
 * @link      http://www.techdivision.com
38
 */
39
class UrlRewriteObserver extends AbstractProductImportObserver
40
{
41
42
    /**
43
     * The entity type to load the URL rewrites for.
44
     *
45
     * @var string
46
     */
47
    const ENTITY_TYPE = 'product';
48
49
    /**
50
     * The URL key from the CSV file column that has to be processed by the observer.
51
     *
52
     * @var string
53
     */
54
    protected $urlKey;
55
56
    /**
57
     * The actual category ID to process.
58
     *
59
     * @var integer
60
     */
61
    protected $categoryId;
62
63
    /**
64
     * The actual entity ID to process.
65
     *
66
     * @var integer
67
     */
68
    protected $entityId;
69
70
    /**
71
     * The array with the URL rewrites that has to be created.
72
     *
73
     * @var array
74
     */
75
    protected $urlRewrites = array();
76
77
    /**
78
     * Process the observer's business logic.
79
     *
80
     * @return void
81
     */
82 3
    protected function process()
83
    {
84
85
        // query whether or not, we've found a new SKU => means we've found a new product
86 3
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::SKU))) {
87
            return;
88
        }
89
90
        // try to load the URL key, return immediately if not possible
91 3
        if ($this->hasValue(ColumnKeys::URL_KEY)) {
92 3
            $this->urlKey = $urlKey = $this->getValue(ColumnKeys::URL_KEY);
93
        } else {
94
            return;
95
        }
96
97
        // initialize the store view code
98 3
        $this->prepareStoreViewCode();
99
100
        // prepare the URL rewrites
101 3
        $this->prepareUrlRewrites();
102
103
        // iterate over the categories and create the URL rewrites
104 3
        foreach ($this->urlRewrites as $categoryId => $urlRewrite) {
105
            // initialize and persist the URL rewrite
106 3
            if ($urlRewrite = $this->initializeUrlRewrite($urlRewrite)) {
107
                // initialize URL rewrite and catagory ID
108 3
                $this->categoryId = $categoryId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $categoryId can also be of type string. However, the property $categoryId is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
109 3
                $this->entityId = $urlRewrite[MemberNames::ENTITY_ID];
110 3
                $this->urlRewriteId = $this->persistUrlRewrite($urlRewrite);
0 ignored issues
show
Bug introduced by
The property urlRewriteId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
111
112
                // initialize and persist the URL rewrite product => category relation
113 3
                $urlRewriteProductCategory = $this->initializeUrlRewriteProductCategory(
114 3
                    $this->prepareUrlRewriteProductCategoryAttributes()
115
                );
116
117
                // persist the URL rewrite product category relation
118 3
                $this->persistUrlRewriteProductCategory($urlRewriteProductCategory);
119
            }
120
        }
121
122
        // if changed, override the URL key with the new one
123 3
        if ($urlKey !== $this->urlKey) {
124
            $this->setValue(ColumnKeys::URL_KEY, $this->urlKey);
125
        }
126 3
    }
127
128
    /**
129
     * Initialize the category product with the passed attributes and returns an instance.
130
     *
131
     * @param array $attr The category product attributes
132
     *
133
     * @return array The initialized category product
134
     */
135 2
    protected function initializeUrlRewrite(array $attr)
136
    {
137 2
        return $attr;
138
    }
139
140
    /**
141
     * Initialize the URL rewrite product => category relation with the passed attributes
142
     * and returns an instance.
143
     *
144
     * @param array $attr The URL rewrite product => category relation attributes
145
     *
146
     * @return array The initialized URL rewrite product => category relation
147
     */
148 2
    protected function initializeUrlRewriteProductCategory($attr)
149
    {
150 2
        return $attr;
151
    }
152
153
    /**
154
     * Prepare's the URL rewrites that has to be created/updated.
155
     *
156
     * @return void
157
     */
158 3
    protected function prepareUrlRewrites()
159
    {
160
161
        // (re-)initialize the array for the URL rewrites
162 3
        $this->urlRewrites = array();
163
164
        // load the root category, because we need that to create the default product URL rewrite
165 3
        $rootCategory = $this->getRootCategory();
166
167
        // query whether or not categories has to be used as product URL suffix
168 3
        $productCategoryIds = array();
169 3
        if ($this->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_PRODUCT_USE_CATEGORIES, false)) {
170
            // if yes, add the category IDs of the products
171 3
            $productCategoryIds = $this->getProductCategoryIds();
172
        }
173
174
        // at least, add the root category ID to the category => product relations
175 3
        $productCategoryIds[$rootCategory[MemberNames::ENTITY_ID]] = $this->getLastEntityId();
176
177
        // prepare the URL rewrites
178 3
        foreach ($productCategoryIds as $categoryId => $entityId) {
179
            // set category/entity ID
180 3
            $this->categoryId = $categoryId;
181 3
            $this->entityId = $entityId;
0 ignored issues
show
Documentation Bug introduced by
The property $entityId was declared of type integer, but $entityId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
182
183
            // prepare the attributes for each URL rewrite
184 3
            $this->urlRewrites[$categoryId] = $this->prepareAttributes();
185
        }
186 3
    }
187
188
    /**
189
     * Make's the passed URL key unique by adding the next number to the end.
190
     *
191
     * @param string $urlKey The URL key to make unique
192
     *
193
     * @return string The unique URL key
194
     */
195 3
    protected function makeUrlKeyUnique($urlKey)
196
    {
197 3
        return $this->getSubject()->makeUrlKeyUnique($urlKey);
198
    }
199
200
    /**
201
     * Prepare the attributes of the entity that has to be persisted.
202
     *
203
     * @return array The prepared attributes
204
     */
205 3
    protected function prepareAttributes()
206
    {
207
208
        // load the store ID to use
209 3
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
210
211
        // load the category to create the URL rewrite for
212 3
        $category = $this->getCategory($this->categoryId);
213
214
        // initialize the values
215 3
        $requestPath = $this->prepareRequestPath($category);
216 3
        $targetPath = $this->prepareTargetPath($category);
217 3
        $metadata = serialize($this->prepareMetadata($category));
218
219
        // return the prepared URL rewrite
220 3
        return $this->initializeEntity(
221
            array(
222 3
                MemberNames::ENTITY_TYPE      => UrlRewriteObserver::ENTITY_TYPE,
223 3
                MemberNames::ENTITY_ID        => $this->entityId,
224 3
                MemberNames::REQUEST_PATH     => $requestPath,
225 3
                MemberNames::TARGET_PATH      => $targetPath,
226 3
                MemberNames::REDIRECT_TYPE    => 0,
227 3
                MemberNames::STORE_ID         => $storeId,
228 3
                MemberNames::DESCRIPTION      => null,
229 3
                MemberNames::IS_AUTOGENERATED => 1,
230 3
                MemberNames::METADATA         => $metadata
231
            )
232
        );
233
    }
234
235
    /**
236
     * Prepare's the URL rewrite product => category relation attributes.
237
     *
238
     * @return arry The prepared attributes
239
     */
240 3
    protected function prepareUrlRewriteProductCategoryAttributes()
241
    {
242
243
        // return the prepared product
244 3
        return $this->initializeEntity(
245
            array(
246 3
                MemberNames::PRODUCT_ID => $this->entityId,
247 3
                MemberNames::CATEGORY_ID => $this->categoryId,
248 3
                MemberNames::URL_REWRITE_ID => $this->urlRewriteId
249
            )
250
        );
251
    }
252
253
    /**
254
     * Prepare's the target path for a URL rewrite.
255
     *
256
     * @param array $category The categroy with the URL path
257
     *
258
     * @return string The target path
259
     */
260 3
    protected function prepareTargetPath(array $category)
261
    {
262
263
        // load the actual entity ID
264 3
        $lastEntityId = $this->getLastEntityId();
265
266
        // query whether or not, the category is the root category
267 3
        if ($this->isRootCategory($category)) {
268 3
            $targetPath = sprintf('catalog/product/view/id/%d', $lastEntityId);
269
        } else {
270 2
            $targetPath = sprintf('catalog/product/view/id/%d/category/%d', $lastEntityId, $category[MemberNames::ENTITY_ID]);
271
        }
272
273
        // return the target path
274 3
        return $targetPath;
275
    }
276
277
    /**
278
     * Prepare's the request path for a URL rewrite or the target path for a 301 redirect.
279
     *
280
     * @param array $category The categroy with the URL path
281
     *
282
     * @return string The request path
283
     */
284 3
    protected function prepareRequestPath(array $category)
285
    {
286
287
        // load the product URL suffix to use
288 3
        $urlSuffix = $this->getCoreConfigData(CoreConfigDataKeys::CATALOG_SEO_PRODUCT_URL_SUFFIX, '.html');
289
290
        // create a unique URL key, if this is a new URL rewrite
291 3
        $this->urlKey = $this->makeUrlKeyUnique($this->urlKey);
292
293
        // query whether or not, the category is the root category
294 3
        if ($this->isRootCategory($category)) {
295 3
            $requestPath = sprintf('%s%s', $this->urlKey, $urlSuffix);
296
        } else {
297 2
            $requestPath = sprintf('%s/%s%s', $category[MemberNames::URL_PATH], $this->urlKey, $urlSuffix);
298
        }
299
300
        // return the request path
301 3
        return $requestPath;
302
    }
303
304
    /**
305
     * Prepare's the URL rewrite's metadata with the passed category values.
306
     *
307
     * @param array $category The category used for preparation
308
     *
309
     * @return array The metadata
310
     */
311 3
    protected function prepareMetadata(array $category)
312
    {
313
314
        // initialize the metadata
315 3
        $metadata = array();
316
317
        // query whether or not, the passed category IS the root category
318 3
        if ($this->isRootCategory($category)) {
319 3
            return $metadata;
320
        }
321
322
        // if not, set the category ID in the metadata
323 2
        $metadata['category_id'] = $category[MemberNames::ENTITY_ID];
324
325
        // return the metadata
326 2
        return $metadata;
327
    }
328
329
    /**
330
     * Initialize's and return's the URL key filter.
331
     *
332
     * @return \TechDivision\Import\Utils\ConvertLiteralUrl The URL key filter
333
     */
334
    protected function getUrlKeyFilter()
335
    {
336
        return new ConvertLiteralUrl();
337
    }
338
339
    /**
340
     * Convert's the passed string into a valid URL key.
341
     *
342
     * @param string $string The string to be converted, e. g. the product name
343
     *
344
     * @return string The converted string as valid URL key
345
     */
346
    protected function convertNameToUrlKey($string)
347
    {
348
        return $this->getUrlKeyFilter()->filter($string);
349
    }
350
351
    /**
352
     * Return's the root category for the actual view store.
353
     *
354
     * @return array The store's root category
355
     * @throws \Exception Is thrown if the root category for the passed store code is NOT available
356
     */
357 3
    protected function getRootCategory()
358
    {
359 3
        return $this->getSubject()->getRootCategory();
360
    }
361
362
    /**
363
     * Return's TRUE if the passed category IS the root category, else FALSE.
364
     *
365
     * @param array $category The category to query
366
     *
367
     * @return boolean TRUE if the passed category IS the root category
368
     */
369 3
    protected function isRootCategory(array $category)
370
    {
371
372
        // load the root category
373 3
        $rootCategory = $this->getRootCategory();
374
375
        // compare the entity IDs and return the result
376 3
        return $rootCategory[MemberNames::ENTITY_ID] === $category[MemberNames::ENTITY_ID];
377
    }
378
379
    /**
380
     * Return's the store ID of the actual row, or of the default store
381
     * if no store view code is set in the CSV file.
382
     *
383
     * @param string|null $default The default store view code to use, if no store view code is set in the CSV file
384
     *
385
     * @return integer The ID of the actual store
386
     * @throws \Exception Is thrown, if the store with the actual code is not available
387
     */
388 3
    protected function getRowStoreId($default = null)
389
    {
390 3
        return $this->getSubject()->getRowStoreId($default);
391
    }
392
393
    /**
394
     * Return's the list with category IDs the product is related with.
395
     *
396
     * @return array The product's category IDs
397
     */
398 3
    protected function getProductCategoryIds()
399
    {
400 3
        return $this->getSubject()->getProductCategoryIds();
401
    }
402
403
    /**
404
     * Return's the category with the passed ID.
405
     *
406
     * @param integer $categoryId The ID of the category to return
407
     *
408
     * @return array The category data
409
     */
410 2
    protected function getCategory($categoryId)
411
    {
412 2
        return $this->getSubject()->getCategory($categoryId);
413
    }
414
415
    /**
416
     * Persist's the URL rewrite with the passed data.
417
     *
418
     * @param array $row The URL rewrite to persist
419
     *
420
     * @return string The ID of the persisted entity
421
     */
422 3
    protected function persistUrlRewrite($row)
423
    {
424 3
        return $this->getSubject()->persistUrlRewrite($row);
425
    }
426
427
    /**
428
     * Persist's the URL rewrite product => category relation with the passed data.
429
     *
430
     * @param array $row The URL rewrite product => category relation to persist
431
     *
432
     * @return void
433
     */
434 3
    protected function persistUrlRewriteProductCategory($row)
435
    {
436 3
        return $this->getSubject()->persistUrlRewriteProductCategory($row);
437
    }
438
}
439