UrlGeneratorStrategy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLocaleNameById() 0 8 2
A cleanupRedirectUrls() 0 6 1
A generate() 0 7 1
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Translator\Generator;
9
10
use Orm\Zed\Locale\Persistence\SpyLocaleQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Locale\Persistence\SpyLocaleQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Orm\Zed\Url\Persistence\SpyUrlQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Url\Persistence\SpyUrlQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Propel\Runtime\ActiveQuery\Criteria;
0 ignored issues
show
Bug introduced by
The type Propel\Runtime\ActiveQuery\Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Facade\AkeneoPimMiddlewareConnectorToUtilTextBridgeInterface;
14
15
class UrlGeneratorStrategy implements UrlGeneratorStrategyInterface
16
{
17
    /**
18
     * @var array<int> Keys are locale ids, values are locale names.
19
     */
20
    protected static $idLocaleBuffer;
21
22
    /**
23
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Facade\AkeneoPimMiddlewareConnectorToUtilTextBridgeInterface
24
     */
25
    protected $utilTextService;
26
27
    /**
28
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Facade\AkeneoPimMiddlewareConnectorToUtilTextBridgeInterface $textService
29
     */
30
    public function __construct(AkeneoPimMiddlewareConnectorToUtilTextBridgeInterface $textService)
31
    {
32
        $this->utilTextService = $textService;
33
    }
34
35
    /**
36
     * @param string $name
37
     * @param int $idLocale
38
     * @param string $identifier
39
     *
40
     * @return string
41
     */
42
    public function generate(string $name, int $idLocale, string $identifier): string
43
    {
44
        $abstractProductUrl = $this->utilTextService->generateSlug($name);
45
        $abstractProductUrl = '/' . substr($this->getLocaleNameById($idLocale), 0, 2) . '/' . $abstractProductUrl . '-' . md5($identifier);
46
        $this->cleanupRedirectUrls($abstractProductUrl);
47
48
        return $abstractProductUrl;
49
    }
50
51
    /**
52
     * @param int $localeId
53
     *
54
     * @return int
55
     */
56
    protected function getLocaleNameById($localeId)
57
    {
58
        if (!isset(static::$idLocaleBuffer[$localeId])) {
59
            static::$idLocaleBuffer[$localeId] =
60
                mb_strtolower(SpyLocaleQuery::create()->findOneByIdLocale($localeId)->getLocaleName());
61
        }
62
63
        return static::$idLocaleBuffer[$localeId];
64
    }
65
66
    /**
67
     * @param string $abstractProductUrl
68
     *
69
     * @return void
70
     */
71
    protected function cleanupRedirectUrls($abstractProductUrl)
72
    {
73
        SpyUrlQuery::create()
74
            ->filterByUrl($abstractProductUrl)
75
            ->filterByFkResourceRedirect(null, Criteria::ISNOTNULL)
76
            ->delete();
77
    }
78
}
79