Skip to content

Commit 2e0d507

Browse files
authored
Merge pull request #231 from dotkernel/issue_230
Fix slug duplication.
2 parents 400c955 + 761510b commit 2e0d507

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

src/Slug/src/Service/SlugService.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
use Ramsey\Uuid\Uuid;
1616
use Ramsey\Uuid\UuidFactory;
1717

18+
19+
/**
20+
* Class SlugService
21+
*
22+
* @package Frontend\Slug\Service
23+
*/
1824
class SlugService implements SlugServiceInterface
1925
{
20-
2126
protected const CONFIGURATION = [
2227
'table',
2328
'identifier',
@@ -69,34 +74,39 @@ function ($matched, $exchange) use ($attribute) {
6974
if ($slug->getType() === Slug::REQUEST_TYPE) {
7075
return $this->processUuidToString($result[$exchange['identifier']]);
7176
}
72-
73-
if (isset($result[$exchange['slugColumn']]) && !is_null($result[$exchange['slugColumn']])) {
74-
return $result[$exchange['slugColumn']];
77+
if ($result[$exchange['exchangeColumn']]) {
78+
if (isset($result[$exchange['slugColumn']]) && !is_null($result[$exchange['slugColumn']])) {
79+
return $result[$exchange['slugColumn']];
80+
} else {
81+
return $this->generateSlug($result, $exchange);
82+
}
7583
} else {
76-
return $this->generateSlug($result, $exchange);
84+
return $value;
7785
}
86+
7887
}
7988
}
8089
return false;
8190
}
8291

8392
/**
8493
* @param array $param
85-
* @param array $exchange
86-
* @return bool|string
94+
* @param array $exchange
95+
* @return string
8796
* @throws \Doctrine\DBAL\Driver\Exception
8897
*/
89-
protected function generateSlug(array $param, array $exchange)
98+
protected function generateSlug(array $param, array $exchange): string
9099
{
91100
$exchangeValue = $param[$exchange['exchangeColumn']];
92101
$exchangeValue = strtolower($exchangeValue);
93-
$exchangeValue = preg_replace('/\s+/', '-', $exchangeValue);
102+
$exchangeValue = str_replace(' ', '-', $exchangeValue);
103+
$exchangeValue = preg_replace('/[^A-Za-z0-9\-]/', '', $exchangeValue);
94104
$exchangeValue = str_replace('.com', '', $exchangeValue);
95105

96106
$response = $this->checkDuplicateSlug($exchangeValue, $exchange);
97107

98108
if ($response) {
99-
$exchangeValue .= '-' . count($response);
109+
$exchangeValue .= '-' . $this->getSlugSuffix($param, $exchange);
100110
}
101111

102112
try {
@@ -106,13 +116,33 @@ protected function generateSlug(array $param, array $exchange)
106116
);
107117
$stmt->bindValue('slug', $this->clean($exchangeValue));
108118
$stmt->bindValue('identifier', $param[$exchange['identifier']]);
109-
$stmt->execute();
119+
$stmt->executeStatement();
110120
return $exchangeValue;
111121
} catch (Exception $exception) {
112122
throw new RuntimeException($exception->getMessage());
113123
}
114124
}
115125

126+
/**
127+
* @param $param
128+
* @param array $exchange
129+
* @return int
130+
*/
131+
protected function getSlugSuffix($param, array $exchange): int
132+
{
133+
try {
134+
$stmt = $this->em->getConnection()->prepare(
135+
'SELECT ' . $exchange['exchangeColumn'] . ' FROM `' . $exchange['table'] . '` WHERE `' .
136+
$exchange['exchangeColumn'] . '` = :exchangeColumn AND `' .
137+
$exchange['slugColumn'] . '` IS NOT NULL'
138+
);
139+
$stmt->bindValue('exchangeColumn', $param[$exchange['exchangeColumn']]);
140+
return $stmt->executeQuery()->rowCount();
141+
} catch (Exception | \Doctrine\DBAL\Driver\Exception $exception) {
142+
throw new RuntimeException($exception->getMessage());
143+
}
144+
}
145+
116146
/**
117147
* @param $input
118148
* @return string
@@ -125,30 +155,28 @@ public function clean($input): string
125155
/**
126156
* @param string $slug
127157
* @param array $exchange
128-
* @return bool|array
158+
* @return int
129159
* @throws \Doctrine\DBAL\Driver\Exception
130160
*/
131-
protected function checkDuplicateSlug(string $slug, array $exchange)
161+
protected function checkDuplicateSlug(string $slug, array $exchange): int
132162
{
133163
try {
134164
$stmt = $this->em->getConnection()->prepare(
135165
'SELECT ' . $exchange['slugColumn'] . ' FROM `' . $exchange['table'] . '` WHERE `' .
136166
$exchange['slugColumn'] . '` = :slug'
137167
);
138168
$stmt->bindValue('slug', $this->clean($slug));
139-
$stmt->execute();
140-
return $stmt->fetchAssociative();
169+
return $stmt->executeQuery()->rowCount();
141170
} catch (Exception $exception) {
142171
throw new RuntimeException($exception->getMessage());
143172
}
144173
}
145174

146-
147175
/**
148176
* @param string $param
149177
* @param array $db
150178
* @param Slug $slug
151-
* @return array|bool
179+
* @return false|array
152180
* @throws \Doctrine\DBAL\Driver\Exception
153181
*/
154182
protected function proceedSlug(Slug $slug, string $param, array $db)
@@ -171,8 +199,7 @@ protected function proceedSlug(Slug $slug, string $param, array $db)
171199
} else {
172200
$stmt->bindValue('searchParam', $param, UuidBinaryOrderedTimeType::NAME);
173201
}
174-
$stmt->execute();
175-
return $stmt->fetchAssociative();
202+
return $stmt->executeQuery()->fetchAssociative();
176203
} catch (Exception $exception) {
177204
throw new RuntimeException($exception->getMessage());
178205
}

0 commit comments

Comments
 (0)