Skip to content

Commit

Permalink
Add three and four level relation handlers (#11)
Browse files Browse the repository at this point in the history
* Add three and four level relation handlers
Upgrade to PHP8

* Update README.md

* Fix unit test issues

* Fix unit test issues

---------

Co-authored-by: mojtaba gheytasi <mojtaba.g@bugloos.com>
  • Loading branch information
mojtaba-gheytasi and mojtaba gheytasi committed Jun 17, 2023
1 parent 29ac3b0 commit cb10f03
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .scrutinizer.yml
Expand Up @@ -19,6 +19,9 @@ checks:
fix_doc_comments: true

build:
image: default-bionic
environment:
php: 8.1.2
tests:
override:
-
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -15,7 +15,7 @@ composer require bugloos/query-sorting-bundle

<h2>Compatibility</h2>

* PHP v7.4 or above
* PHP v8.1 or above
* Symfony v4.4 or above

<h2>Usage</h2>
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -12,13 +12,14 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=8.1",
"doctrine/doctrine-bundle": "^2.2",
"doctrine/orm": "^2.8",
"symfony/http-kernel": "^4.4 | ^5.4 | ^6.0",
"psr/cache": "^1.0 | ^2.0 | ^3.0",
"symfony/orm-pack": "^2.2",
"symfony/framework-bundle": "^4.4 | ^5.4 | ^6.0"
"symfony/framework-bundle": "^4.4 | ^5.4 | ^6.0",
"doctrine/annotations": "^2.0"
},
"require-dev": {
"ext-json": "*",
Expand Down
10 changes: 10 additions & 0 deletions src/SortingHandler/Factory/SortingFactory.php
Expand Up @@ -8,6 +8,8 @@
use Bugloos\QuerySortingBundle\SortingHandler\NoRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\OneLevelRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\TwoLevelRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\ThreeLevelRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\FourLevelRelationHandler;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -18,6 +20,8 @@ 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;

protected EntityManagerInterface $entityManager;

Expand All @@ -38,6 +42,12 @@ public function createSortingHandler(array $relationsAndFieldName): AbstractSort
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'
Expand Down
48 changes: 48 additions & 0 deletions src/SortingHandler/FourLevelRelationHandler.php
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Bugloos\QuerySortingBundle\SortingHandler;

use Bugloos\QuerySortingBundle\SortingHandler\Contract\AbstractSortingHandler;
use Bugloos\QuerySortingBundle\SortingHandler\Contract\WithRelationInterface;

/**
* @author Milad Ghofrani <milad.ghofrani@gmail.com>
*/
class FourLevelRelationHandler extends AbstractSortingHandler implements WithRelationInterface
{
public function getSortProperty($rootAlias, $rootClass, $relationsAndFieldName): string
{
[$relationAlias, $secondRelationAlias, $thirdRelationAlias, $fourthRelationAlias, $subRelationField] = $relationsAndFieldName;

$this->validateRelationNames($relationAlias, $rootClass);

$relationClass = $this->getRelationClass($rootClass, $relationAlias);
$this->validateRelationNames($secondRelationAlias, $relationClass);

$secondRelationClass = $this->getRelationClass($relationClass, $secondRelationAlias);
$this->validateRelationNames($subRelationField, $secondRelationClass);

$thirdRelationClass = $this->getRelationClass($secondRelationClass, $thirdRelationAlias);
$this->validateFieldNames($subRelationField, $thirdRelationClass);

$fourthRelationClass = $this->getRelationClass($thirdRelationClass, $fourthRelationAlias);
$this->validateFieldNames($subRelationField, $fourthRelationClass);

return $this->createOrderBySyntax($fourthRelationAlias, $subRelationField);
}

public function getRelationJoin($relationJoins, $rootAlias, $rootClass, $relationsAndFieldName): array
{
[$relationAlias, $subRelationAlias, $thirdRelationAlias, $fourthRelationAlias] = $relationsAndFieldName;

$relationJoins = $this->addRelationJoin($relationJoins, $rootAlias, $relationAlias);

$relationJoins = $this->addRelationJoin($relationJoins, $relationAlias, $subRelationAlias);

$relationJoins = $this->addRelationJoin($relationJoins, $subRelationAlias, $thirdRelationAlias);

return $this->addRelationJoin($relationJoins, $thirdRelationAlias, $fourthRelationAlias);
}
}
43 changes: 43 additions & 0 deletions src/SortingHandler/ThreeLevelRelationHandler.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Bugloos\QuerySortingBundle\SortingHandler;

use Bugloos\QuerySortingBundle\SortingHandler\Contract\AbstractSortingHandler;
use Bugloos\QuerySortingBundle\SortingHandler\Contract\WithRelationInterface;

/**
* @author Milad Ghofrani <milad.ghofrani@gmail.com>
*/
class ThreeLevelRelationHandler extends AbstractSortingHandler implements WithRelationInterface
{
public function getSortProperty($rootAlias, $rootClass, $relationsAndFieldName): string
{
[$relationAlias, $secondRelationAlias, $thirdRelationAlias, $subRelationField] = $relationsAndFieldName;

$this->validateRelationNames($relationAlias, $rootClass);

$relationClass = $this->getRelationClass($rootClass, $relationAlias);
$this->validateRelationNames($secondRelationAlias, $relationClass);

$secondRelationClass = $this->getRelationClass($relationClass, $secondRelationAlias);
$this->validateRelationNames($subRelationField, $secondRelationClass);

$thirdRelationClass = $this->getRelationClass($secondRelationClass, $thirdRelationAlias);
$this->validateFieldNames($subRelationField, $thirdRelationClass);

return $this->createOrderBySyntax($thirdRelationAlias, $subRelationField);
}

public function getRelationJoin($relationJoins, $rootAlias, $rootClass, $relationsAndFieldName): array
{
[$relationAlias, $subRelationAlias, $thirdRelationAlias] = $relationsAndFieldName;

$relationJoins = $this->addRelationJoin($relationJoins, $rootAlias, $relationAlias);

$relationJoins = $this->addRelationJoin($relationJoins, $relationAlias, $subRelationAlias);

return $this->addRelationJoin($relationJoins, $subRelationAlias, $thirdRelationAlias);
}
}
38 changes: 36 additions & 2 deletions tests/Unit/SortingHandler/Factory/SortingFactoryTest.php
Expand Up @@ -3,8 +3,10 @@
namespace Bugloos\QuerySortingBundle\Tests\Unit\SortingHandler\Factory;

use Bugloos\QuerySortingBundle\SortingHandler\Factory\SortingFactory;
use Bugloos\QuerySortingBundle\SortingHandler\FourLevelRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\NoRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\OneLevelRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\ThreeLevelRelationHandler;
use Bugloos\QuerySortingBundle\SortingHandler\TwoLevelRelationHandler;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -61,7 +63,39 @@ public function test_relation_handler_with_two_level_relation(): void
self::assertInstanceOf(TwoLevelRelationHandler::class, $relationHandler);
}

public function test_relation_handler_with_an_exception_when_need_relation_more_than_two_level(): void
public function test_relation_handler_with_three_level_relation(): void
{
$entityManager = $this
->getMockBuilder('Doctrine\ORM\EntityManager')
->setMethods(['getRepository'])
->disableOriginalConstructor()
->getMock()
;

$sortingFactory = new SortingFactory($entityManager);

$relationHandler = $sortingFactory->createSortingHandler(['bookUsers', 'user', 'age', 'fake']);

self::assertInstanceOf(ThreeLevelRelationHandler::class, $relationHandler);
}

public function test_relation_handler_with_four_level_relation(): void
{
$entityManager = $this
->getMockBuilder('Doctrine\ORM\EntityManager')
->setMethods(['getRepository'])
->disableOriginalConstructor()
->getMock()
;

$sortingFactory = new SortingFactory($entityManager);

$relationHandler = $sortingFactory->createSortingHandler(['bookUsers', 'user', 'age', 'fake', 'fake']);

self::assertInstanceOf(FourLevelRelationHandler::class, $relationHandler);
}

public function test_relation_handler_with_an_exception_when_need_relation_more_than_four_level(): void
{
$entityManager = $this
->getMockBuilder('Doctrine\ORM\EntityManager')
Expand All @@ -74,6 +108,6 @@ public function test_relation_handler_with_an_exception_when_need_relation_more_

$this->expectException(\RuntimeException::class);

$sortingFactory->createSortingHandler(['bookUsers', 'user', 'country', 'name']);
$sortingFactory->createSortingHandler(['bookUsers', 'user', 'country', 'name', 'fake', 'fake']);
}
}

0 comments on commit cb10f03

Please sign in to comment.