PaginationTypeFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
eloc 21
c 4
b 0
f 0
dl 0
loc 55
ccs 0
cts 24
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 19 4
A canCreate() 0 5 3
A __invoke() 0 12 2
A getExtraFields() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Api\Output;
6
7
use Ecodev\Felix\Model\Model;
8
use Exception;
9
use GraphQL\Type\Definition\ObjectType;
10
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
11
use Psr\Container\ContainerInterface;
12
13
/**
14
 * Create a Pagination type for a Doctrine entity or an ObjectType extracted from name.
15
 *
16
 * For example, if given "ActionPagination", it will create a Pagination type for the Action entity.
17
 * Alternatively, if given "ReportRowPagination", it will create a Pagination type for the custom `OutputType` of `ReportRowType`.
18
 */
19
class PaginationTypeFactory implements AbstractFactoryInterface
20
{
21
    private const PATTERN = '~^(.*)Pagination$~';
22
23
    public function canCreate(ContainerInterface $container, $requestedName): bool
24
    {
25
        $class = $this->getClass($requestedName);
26
27
        return $class && (is_a($class, Model::class, true) || is_a($class, ObjectType::class, true));
28
    }
29
30
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): PaginationType
31
    {
32
        $class = $this->getClass($requestedName);
33
        if (!$class) {
34
            throw new Exception('Cannot create a PaginationType for a name not matching a model: ' . $requestedName);
35
        }
36
37
        $extraFields = $this->getExtraFields($class);
38
39
        $type = new PaginationType($class, $requestedName, $extraFields);
40
41
        return $type;
42
    }
43
44
    /**
45
     * @return null|class-string
46
     */
47
    private function getClass(string $requestedName): ?string
48
    {
49
        if (!preg_match(self::PATTERN, $requestedName, $m)) {
50
            return null;
51
        }
52
53
        $possibilities = [
54
            'Application\Model\\' . $m[1],
55
            'Application\Model\Relation\\' . $m[1],
56
            'Application\Api\Output\\' . $m[1] . 'Type',
57
        ];
58
59
        foreach ($possibilities as $class) {
60
            if (class_exists($class)) {
61
                return $class;
62
            }
63
        }
64
65
        return null;
66
    }
67
68
    /**
69
     * GraphQL configuration for extra fields, typically for aggregated fields only available on some entities.
70
     */
71
    protected function getExtraFields(string $class): array
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
    protected function getExtraFields(/** @scrutinizer ignore-unused */ string $class): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
        return [];
74
    }
75
}
76