Skip to content

Commit

Permalink
Merge pull request #231 from dotkernel/issue_230
Browse files Browse the repository at this point in the history
Fix slug duplication.
  • Loading branch information
arhimede authored Jun 16, 2021
2 parents 400c955 + 761510b commit 2e0d507
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions src/Slug/src/Service/SlugService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactory;


/**
* Class SlugService
*
* @package Frontend\Slug\Service
*/
class SlugService implements SlugServiceInterface
{

protected const CONFIGURATION = [
'table',
'identifier',
Expand Down Expand Up @@ -69,34 +74,39 @@ function ($matched, $exchange) use ($attribute) {
if ($slug->getType() === Slug::REQUEST_TYPE) {
return $this->processUuidToString($result[$exchange['identifier']]);
}

if (isset($result[$exchange['slugColumn']]) && !is_null($result[$exchange['slugColumn']])) {
return $result[$exchange['slugColumn']];
if ($result[$exchange['exchangeColumn']]) {
if (isset($result[$exchange['slugColumn']]) && !is_null($result[$exchange['slugColumn']])) {
return $result[$exchange['slugColumn']];
} else {
return $this->generateSlug($result, $exchange);
}
} else {
return $this->generateSlug($result, $exchange);
return $value;
}

}
}
return false;
}

/**
* @param array $param
* @param array $exchange
* @return bool|string
* @param array $exchange
* @return string
* @throws \Doctrine\DBAL\Driver\Exception
*/
protected function generateSlug(array $param, array $exchange)
protected function generateSlug(array $param, array $exchange): string
{
$exchangeValue = $param[$exchange['exchangeColumn']];
$exchangeValue = strtolower($exchangeValue);
$exchangeValue = preg_replace('/\s+/', '-', $exchangeValue);
$exchangeValue = str_replace(' ', '-', $exchangeValue);
$exchangeValue = preg_replace('/[^A-Za-z0-9\-]/', '', $exchangeValue);
$exchangeValue = str_replace('.com', '', $exchangeValue);

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

if ($response) {
$exchangeValue .= '-' . count($response);
$exchangeValue .= '-' . $this->getSlugSuffix($param, $exchange);
}

try {
Expand All @@ -106,13 +116,33 @@ protected function generateSlug(array $param, array $exchange)
);
$stmt->bindValue('slug', $this->clean($exchangeValue));
$stmt->bindValue('identifier', $param[$exchange['identifier']]);
$stmt->execute();
$stmt->executeStatement();
return $exchangeValue;
} catch (Exception $exception) {
throw new RuntimeException($exception->getMessage());
}
}

/**
* @param $param
* @param array $exchange
* @return int
*/
protected function getSlugSuffix($param, array $exchange): int
{
try {
$stmt = $this->em->getConnection()->prepare(
'SELECT ' . $exchange['exchangeColumn'] . ' FROM `' . $exchange['table'] . '` WHERE `' .
$exchange['exchangeColumn'] . '` = :exchangeColumn AND `' .
$exchange['slugColumn'] . '` IS NOT NULL'
);
$stmt->bindValue('exchangeColumn', $param[$exchange['exchangeColumn']]);
return $stmt->executeQuery()->rowCount();
} catch (Exception | \Doctrine\DBAL\Driver\Exception $exception) {
throw new RuntimeException($exception->getMessage());
}
}

/**
* @param $input
* @return string
Expand All @@ -125,30 +155,28 @@ public function clean($input): string
/**
* @param string $slug
* @param array $exchange
* @return bool|array
* @return int
* @throws \Doctrine\DBAL\Driver\Exception
*/
protected function checkDuplicateSlug(string $slug, array $exchange)
protected function checkDuplicateSlug(string $slug, array $exchange): int
{
try {
$stmt = $this->em->getConnection()->prepare(
'SELECT ' . $exchange['slugColumn'] . ' FROM `' . $exchange['table'] . '` WHERE `' .
$exchange['slugColumn'] . '` = :slug'
);
$stmt->bindValue('slug', $this->clean($slug));
$stmt->execute();
return $stmt->fetchAssociative();
return $stmt->executeQuery()->rowCount();
} catch (Exception $exception) {
throw new RuntimeException($exception->getMessage());
}
}


/**
* @param string $param
* @param array $db
* @param Slug $slug
* @return array|bool
* @return false|array
* @throws \Doctrine\DBAL\Driver\Exception
*/
protected function proceedSlug(Slug $slug, string $param, array $db)
Expand All @@ -171,8 +199,7 @@ protected function proceedSlug(Slug $slug, string $param, array $db)
} else {
$stmt->bindValue('searchParam', $param, UuidBinaryOrderedTimeType::NAME);
}
$stmt->execute();
return $stmt->fetchAssociative();
return $stmt->executeQuery()->fetchAssociative();
} catch (Exception $exception) {
throw new RuntimeException($exception->getMessage());
}
Expand Down

0 comments on commit 2e0d507

Please sign in to comment.