Completed
Push — 15.x ( 1a7f4e...53b7d0 )
by Tim
65:00 queued 61:58
created

UrlKeyUtil::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\UrlKeyUtil
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 2019 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Utils;
22
23
use TechDivision\Import\ConfigurationInterface;
24
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface;
25
use TechDivision\Import\Services\UrlKeyAwareProcessorInterface;
26
27
/**
28
 * Utility class that provides functionality to make URL keys unique.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class UrlKeyUtil implements UrlKeyUtilInterface
37
{
38
39
    /**
40
     * The configuration instance.
41
     *
42
     * @var \TechDivision\Import\ConfigurationInterface
43
     */
44
    protected $configuration;
45
46
    /**
47
     * The URL key aware processor instance.
48
     *
49
     * \TechDivision\Import\Services\UrlKeyAwareProcessorInterface
50
     */
51
    protected $urlKeyAwareProcessor;
52
53
    /**
54
     * Construct a new instance.
55
     *
56
     * @param \TechDivision\Import\ConfigurationInterface                 $configuration        The configuration instance
57
     * @param \TechDivision\Import\Services\UrlKeyAwareProcessorInterface $urlKeyAwareProcessor The URL key aware processor instance
58
     */
59
    public function __construct(
60
        ConfigurationInterface $configuration,
61
        UrlKeyAwareProcessorInterface $urlKeyAwareProcessor
62
    ) {
63
        $this->configuration = $configuration;
64
        $this->urlKeyAwareProcessor = $urlKeyAwareProcessor;
65
    }
66
67
    /**
68
     * Returns the URL key aware processor instance.
69
     *
70
     * @return \TechDivision\Import\Services\UrlKeyAwareProcessorInterface The processor instance
71
     */
72
    protected function getUrlKeyAwareProcessor()
73
    {
74
        return $this->urlKeyAwareProcessor;
75
    }
76
77
    /**
78
     * Load's and return's the varchar attribute with the passed params.
79
     *
80
     * @param integer $attributeCode The attribute code of the varchar attribute
81
     * @param integer $entityTypeId  The entity type ID of the varchar attribute
82
     * @param integer $storeId       The store ID of the varchar attribute
83
     * @param string  $value         The value of the varchar attribute
84
     *
85
     * @return array|null The varchar attribute
86
     */
87
    protected function loadVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value)
88
    {
89
        return $this->getUrlKeyAwareProcessor()
90
                    ->loadVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue(
91
                        $attributeCode,
92
                        $entityTypeId,
93
                        $storeId,
94
                        $value
95
                    );
96
    }
97
98
    /**
99
     * Make's the passed URL key unique by adding the next number to the end.
100
     *
101
     * @param string $urlKey The URL key to make unique
102
     *
103
     * @return string The unique URL key
104
     */
105
    public function makeUnique(UrlKeyAwareSubjectInterface $subject, $urlKey)
106
    {
107
108
        // initialize the entity type ID
109
        $entityType = $subject->getEntityType();
110
        $entityTypeId = (integer) $entityType[MemberNames::ENTITY_TYPE_ID];
111
112
        // initialize the store view ID, use the admin store view if no store view has
113
        // been set, because the default url_key value has been set in admin store view
114
        $storeId = $subject->getRowStoreId(StoreViewCodes::ADMIN);
115
116
        // initialize the counter
117
        $counter = 0;
118
119
        // initialize the counters
120
        $matchingCounters = array();
121
        $notMatchingCounters = array();
122
123
        // pre-initialze the URL key to query for
124
        $value = $urlKey;
125
126
        do {
127
            // try to load the attribute
128
            $attribute =
129
                $this->loadVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue(
130
                    MemberNames::URL_KEY,
131
                    $entityTypeId,
132
                    $storeId,
133
                    $value
134
                );
135
136
            // try to load the entity's URL key
137
            if ($attribute) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attribute of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
138
                // this IS the URL key of the passed entity
139
                if ($subject->isUrlKeyOf($attribute)) {
140
                    $matchingCounters[] = $counter;
141
                } else {
142
                    $notMatchingCounters[] = $counter;
143
                }
144
145
                // prepare the next URL key to query for
146
                $value = sprintf('%s-%d', $urlKey, ++$counter);
147
            }
148
        } while ($attribute);
0 ignored issues
show
Bug Best Practice introduced by
The expression $attribute of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
149
150
        // sort the array ascending according to the counter
151
        asort($matchingCounters);
152
        asort($notMatchingCounters);
153
154
        // this IS the URL key of the passed entity => we've an UPDATE
155
        if (sizeof($matchingCounters) > 0) {
156
            // load highest counter
157
            $counter = end($matchingCounters);
158
            // if the counter is > 0, we've to append it to the new URL key
159
            if ($counter > 0) {
160
                $urlKey = sprintf('%s-%d', $urlKey, $counter);
161
            }
162
        } elseif (sizeof($notMatchingCounters) > 0) {
163
            // create a new URL key by raising the counter
164
            $newCounter = end($notMatchingCounters);
165
            $urlKey = sprintf('%s-%d', $urlKey, ++$newCounter);
166
        }
167
168
        // return the passed URL key, if NOT
169
        return $urlKey;
170
    }
171
}
172