InstitutionLocality   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 6
dl 0
loc 15
c 1
b 0
f 0
ccs 0
cts 6
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __invoke() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Input\Sorting;
6
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\QueryBuilder;
9
use GraphQL\Doctrine\Factory\UniqueNameFactory;
10
use GraphQL\Doctrine\Sorting\SortingInterface;
11
12
class InstitutionLocality implements SortingInterface
13
{
14
    public function __construct() {}
15
16
    public function __invoke(UniqueNameFactory $uniqueNameFactory, ClassMetadata $metadata, QueryBuilder $queryBuilder, string $alias, string $order): void
17
    {
18
        $queryBuilder->leftJoin($alias . '.institution', 'sortingInstitution');
19
20
        // First keep card without any institution at the bottom of the list
21
        $sortingFieldNullAsHighest = $uniqueNameFactory->createAliasName('sorting');
22
        $queryBuilder->addSelect("CASE WHEN sortingInstitution.locality IS NULL OR sortingInstitution.locality = '' THEN 1 ELSE 0 END AS HIDDEN " . $sortingFieldNullAsHighest);
23
        $queryBuilder->addOrderBy($sortingFieldNullAsHighest, $order);
24
25
        // Then sort cards with institutions
26
        $queryBuilder->addOrderBy('sortingInstitution.locality', $order);
27
    }
28
}
29