Skip to content

Commit

Permalink
Improve SortingFactory.php (#12)
Browse files Browse the repository at this point in the history
* Update error response message
* Improve handler to support levels dynamically (remove  switch)
  • Loading branch information
aminkhoshzahmat committed Aug 6, 2023
1 parent cb10f03 commit 643928d
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/SortingHandler/Factory/SortingFactory.php
Expand Up @@ -14,14 +14,17 @@

/**
* @author Milad Ghofrani <milad.ghofrani@gmail.com>
* @contributor Amin Khoshzahmat <aminkhoshzahmat@gmail.com>
*/
class SortingFactory
{
private const NO_RELATION = 1;
private const ONE_LEVEL_RELATION = 2;
private const TWO_LEVEL_RELATION = 3;
private const THREE_LEVEL_RELATION = 4;
private const FOUR_LEVEL_RELATION = 5;
private const HANDLER_MAP = [
1 => NoRelationHandler::class,
2 => OneLevelRelationHandler::class,
3 => TwoLevelRelationHandler::class,
4 => ThreeLevelRelationHandler::class,
5 => FourLevelRelationHandler::class,
];

protected EntityManagerInterface $entityManager;

Expand All @@ -32,26 +35,15 @@ public function __construct(EntityManagerInterface $entityManager)

public function createSortingHandler(array $relationsAndFieldName): AbstractSortingHandler
{
switch (\count($relationsAndFieldName)) {
case self::NO_RELATION:
return new NoRelationHandler($this->entityManager);
$count = \count($relationsAndFieldName);

case self::ONE_LEVEL_RELATION:
return new OneLevelRelationHandler($this->entityManager);

case self::TWO_LEVEL_RELATION:
return new TwoLevelRelationHandler($this->entityManager);

case self::THREE_LEVEL_RELATION:
return new ThreeLevelRelationHandler($this->entityManager);

case self::FOUR_LEVEL_RELATION:
return new FourLevelRelationHandler($this->entityManager);

default:
throw new \RuntimeException(
'This Bundle just support maximum two level relation'
);
if (!isset(self::HANDLER_MAP[$count])) {
throw new \RuntimeException(
sprintf('Unsupported relation level: %d. This Bundle supports up to a maximum of four level relations.', $count)
);
}

$handlerClass = self::HANDLER_MAP[$count];
return new $handlerClass($this->entityManager);
}
}

0 comments on commit 643928d

Please sign in to comment.